sort array of objects if the object has a property
up vote
1
down vote
favorite
I have an object like this:
"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
],
I have tried many ways to sort it...Need some help
If this array has a "newOne" property I Want that object to be first in the array etc'...
If you have 3 objects with the proprty "newOne" so they will be the first to show one after the other and not that the next mapping object will be at the top of the array.
I am using React if that means anything
Thanks!
javascript arrays json reactjs sorting
add a comment |
up vote
1
down vote
favorite
I have an object like this:
"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
],
I have tried many ways to sort it...Need some help
If this array has a "newOne" property I Want that object to be first in the array etc'...
If you have 3 objects with the proprty "newOne" so they will be the first to show one after the other and not that the next mapping object will be at the top of the array.
I am using React if that means anything
Thanks!
javascript arrays json reactjs sorting
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an object like this:
"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
],
I have tried many ways to sort it...Need some help
If this array has a "newOne" property I Want that object to be first in the array etc'...
If you have 3 objects with the proprty "newOne" so they will be the first to show one after the other and not that the next mapping object will be at the top of the array.
I am using React if that means anything
Thanks!
javascript arrays json reactjs sorting
I have an object like this:
"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
],
I have tried many ways to sort it...Need some help
If this array has a "newOne" property I Want that object to be first in the array etc'...
If you have 3 objects with the proprty "newOne" so they will be the first to show one after the other and not that the next mapping object will be at the top of the array.
I am using React if that means anything
Thanks!
javascript arrays json reactjs sorting
javascript arrays json reactjs sorting
asked Nov 19 at 14:50
user1102152
2617
2617
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
5
down vote
You could check for the property and take the check for the delta for sorting.
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
add a comment |
up vote
0
down vote
You can use the function unshift from array to achieve your goal.
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
Same could be achieved with reduce:
const result = data.reduce((agg, itr) => {
if (itr.newOne) agg.unshift(itr)
else agg.push(itr)
return agg
}, )
console.log(result)
add a comment |
up vote
0
down vote
This should be doable by treating items with a set newOne
property as greater in value than those that don't.
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
add a comment |
up vote
0
down vote
Here is another sorting function without using array in
:
data.sort((a,b) => a.newOne === b.newOne ? 0 : a.newOne ? -1 : 1)
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
You could check for the property and take the check for the delta for sorting.
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
add a comment |
up vote
5
down vote
You could check for the property and take the check for the delta for sorting.
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
add a comment |
up vote
5
down vote
up vote
5
down vote
You could check for the property and take the check for the delta for sorting.
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could check for the property and take the check for the delta for sorting.
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var data = [{ name: "name1", price: 4.2 }, { name: "name2", price: 12.9, newOne: {} }, { name: "name3", price: 10.9, newOne: { code: "02ec583021de8e36ae8006c3caef72d9", name: "מבצע 13" } }, { name: "name3", price: 10.9 }];
data.sort((a, b) => ('newOne' in b) - ('newOne' in a));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 19 at 14:53
Nina Scholz
172k1384147
172k1384147
1
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
add a comment |
1
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
1
1
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
TIL maths on booleans will implicitly coerce them to ints. Thank you! :-D
– dgeare
Nov 19 at 14:59
add a comment |
up vote
0
down vote
You can use the function unshift from array to achieve your goal.
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
Same could be achieved with reduce:
const result = data.reduce((agg, itr) => {
if (itr.newOne) agg.unshift(itr)
else agg.push(itr)
return agg
}, )
console.log(result)
add a comment |
up vote
0
down vote
You can use the function unshift from array to achieve your goal.
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
Same could be achieved with reduce:
const result = data.reduce((agg, itr) => {
if (itr.newOne) agg.unshift(itr)
else agg.push(itr)
return agg
}, )
console.log(result)
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use the function unshift from array to achieve your goal.
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
Same could be achieved with reduce:
const result = data.reduce((agg, itr) => {
if (itr.newOne) agg.unshift(itr)
else agg.push(itr)
return agg
}, )
console.log(result)
You can use the function unshift from array to achieve your goal.
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
Same could be achieved with reduce:
const result = data.reduce((agg, itr) => {
if (itr.newOne) agg.unshift(itr)
else agg.push(itr)
return agg
}, )
console.log(result)
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
const data =
[
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]
let result =
data.forEach(item => {
if (item.newOne) result.unshift(item)
else result.push(item)
})
console.log(result)
answered Nov 19 at 14:56
omri_saadon
6,95031444
6,95031444
add a comment |
add a comment |
up vote
0
down vote
This should be doable by treating items with a set newOne
property as greater in value than those that don't.
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
add a comment |
up vote
0
down vote
This should be doable by treating items with a set newOne
property as greater in value than those that don't.
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
add a comment |
up vote
0
down vote
up vote
0
down vote
This should be doable by treating items with a set newOne
property as greater in value than those that don't.
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
This should be doable by treating items with a set newOne
property as greater in value than those that don't.
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
var t = {"data": [
{
"name": "name1",
"price": 4.2,
},
{
"name": "name2",
"price": 12.9,
"newOne": {}
},
{
"name": "name3",
"price": 10.9,
"newOne": {
"code": "02ec583021de8e36ae8006c3caef72d9",
"name": "מבצע 13"
}
},
{
"name": "name3",
"price": 10.9,
},
]};
t.data.sort(function(a,b){
var aVal = a.newOne == undefined ? 0 : 1;
var bVal = b.newOne == undefined ? 0 : 1;
return bVal - aVal;
});
console.log(t.data);
answered Nov 19 at 14:58
dgeare
2,03731222
2,03731222
add a comment |
add a comment |
up vote
0
down vote
Here is another sorting function without using array in
:
data.sort((a,b) => a.newOne === b.newOne ? 0 : a.newOne ? -1 : 1)
add a comment |
up vote
0
down vote
Here is another sorting function without using array in
:
data.sort((a,b) => a.newOne === b.newOne ? 0 : a.newOne ? -1 : 1)
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is another sorting function without using array in
:
data.sort((a,b) => a.newOne === b.newOne ? 0 : a.newOne ? -1 : 1)
Here is another sorting function without using array in
:
data.sort((a,b) => a.newOne === b.newOne ? 0 : a.newOne ? -1 : 1)
answered Nov 19 at 15:00
Goran.it
3,20711620
3,20711620
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377136%2fsort-array-of-objects-if-the-object-has-a-property%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown