I want to sort a object in javascript. I have this json:
var unsorted = {
"2014": [
{
"title": "Book",
"value": 10
},
{
"title": "Cheese",
"value": 2
}
],
"2015": [
{
"title": "Movie",
"value": 9
}
],
"2016": [
{
"title": "Slay",
"value": 90
},
{
"title": "Back",
"value": 22
}
],
"selected_year": 2015
};
So I want to sort the jsons inside these years based on the value. I tried it with this:
var sorted = unsorted.items.sort(function(a, b) {return a.value - b.value});
alert.log(unsorted);
alert.log(sorted);
but the problem is that I have another field which is not json "selected_year": 2015
and it throat an error TypeError: Cannot read property 'sort' of undefined
I don't know how to do it? Here's my JSFIDDLE
UPDATE: I want to sort the arrays inside "years" so I get this:
var sorted = {
"2014": [
{
"title": "Cheese",
"value": 2
},
{
"title": "Book",
"value": 10
}
],
"2015": [
{
"title": "Movie",
"value": 9
}
],
"2016": [
{
"title": "Back",
"value": 22
},
{
"title": "Slay",
"value": 90
}
],
"selected_year": 2015
};
for example inside "2014" after sorting I have:
"2014": [
{
"title": "Cheese",
"value": 2
},
{
"title": "Book",
"value": 10
}
]
I want to sort a object in javascript. I have this json:
var unsorted = {
"2014": [
{
"title": "Book",
"value": 10
},
{
"title": "Cheese",
"value": 2
}
],
"2015": [
{
"title": "Movie",
"value": 9
}
],
"2016": [
{
"title": "Slay",
"value": 90
},
{
"title": "Back",
"value": 22
}
],
"selected_year": 2015
};
So I want to sort the jsons inside these years based on the value. I tried it with this:
var sorted = unsorted.items.sort(function(a, b) {return a.value - b.value});
alert.log(unsorted);
alert.log(sorted);
but the problem is that I have another field which is not json "selected_year": 2015
and it throat an error TypeError: Cannot read property 'sort' of undefined
I don't know how to do it? Here's my JSFIDDLE
UPDATE: I want to sort the arrays inside "years" so I get this:
var sorted = {
"2014": [
{
"title": "Cheese",
"value": 2
},
{
"title": "Book",
"value": 10
}
],
"2015": [
{
"title": "Movie",
"value": 9
}
],
"2016": [
{
"title": "Back",
"value": 22
},
{
"title": "Slay",
"value": 90
}
],
"selected_year": 2015
};
for example inside "2014" after sorting I have:
"2014": [
{
"title": "Cheese",
"value": 2
},
{
"title": "Book",
"value": 10
}
]
Share
Improve this question
edited Dec 11, 2017 at 10:08
Babel
asked Dec 11, 2017 at 10:01
BabelBabel
6192 gold badges10 silver badges23 bronze badges
10
-
1
sort is Array's method,
unsorted
is a simple plain vanilla js object. – gurvinder372 Commented Dec 11, 2017 at 10:03 - Just to be clear about what you're trying to do, objects have no concept of "sorted". But the visual representation can be rearranged, if that's your goal. – Andrew Commented Dec 11, 2017 at 10:05
- sort() is an array method, your data is a dictionary. You might want to reconsider your data structure or write your own sorting function – mike_t Commented Dec 11, 2017 at 10:05
- which one would you like to sort? sort sorts in situ. – Nina Scholz Commented Dec 11, 2017 at 10:06
-
1
Strange!! that no one of the upvoted answers pointed to the real problem in your code!!! Actually there's no
unsorted.items
. :o – cнŝdk Commented Dec 11, 2017 at 10:39
5 Answers
Reset to default 2You can sort only array properties:
var unsorted = {
"2014": [
{
"title": "Book",
"value": 10
},
{
"title": "Cheese",
"value": 2
}
],
"2015": [
{
"title": "Movie",
"value": 9
}
],
"2016": [
{
"title": "Slay",
"value": 90
},
{
"title": "Back",
"value": 22
}
],
"selected_year": 2015
};
var sorted = Object.assign({}, unsorted); // Copy the unsorted object
Object.keys(sorted).forEach(key => {
if (Array.isArray(sorted[key])) {
sorted[key].sort((a, b) => a.value - b.value);
}
})
console.log(sorted);
Only sort those values which are array. You can check for array using Array.isArray
.
var unsorted = { 2014: [{ title: "Book", value: 10 }, { title: "Cheese", value: 2 }], 2015: [{ title: "Movie", value: 9 }], 2016: [{ title: "Slay", value: 90 }, { title: "Back", value: 22 }], selected_year: 2015 };
Object
.keys(unsorted)
.forEach(k => {
if(Array.isArray(unsorted[k]))
unsorted[k].sort((a,b) => a.value - b.value);
});
console.log(unsorted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could check if the property is an array and sort it as desired.
var object = { 2014: [{ title: "Book", value: 10 }, { title: "Cheese", value: 2 }], 2015: [{ title: "Movie", value: 9 }], 2016: [{ title: "Slay", value: 90 }, { title: "Back", value: 22 }], selected_year: 2015 };
Object.keys(object).forEach(function (k) {
if (!Array.isArray(object[k])) {
return;
}
object[k].sort(function (a, b) {
return a.value - b.value;
});
});
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You are mixing array of object
and object
that why you are getting this error, for selected_yea
TypeError: Cannot read property 'sort' of undefined
Sort it like this
var unsorted = { 2014: [{ title: "Book", value: 10 }, { title: "Cheese", value: 2 }], 2015: [{ title: "Movie", value: 9 }], 2016: [{ title: "Slay", value: 90 }, { title: "Back", value: 22 }], selected_year: 2015 };
var newArr = [];
for(let i in unsorted){
if(i !== 'selected_year'){
unsorted[i] = unsorted[i].sort(function(a, b) {
return a.value - b.value;
});
}
}
console.log(unsorted);
This solution is more faster than others.
Check Time taken of this : https://jsfiddle/q976tk4r/
Other solutions : https://jsfiddle/q976tk4r/1/ or https://jsfiddle/q976tk4r/2/
Problem:
Actually there's no such property unsorted.items
in your JSON object, there are several keys for years which can be accessed using Object.keys(unsorted)
.
Solution:
So loop over these elements with a .map()
and check if the element is an array
with Array.isArray()
return it sorted, otherwise just return the value:
var sorted = Object.keys(unsorted).map(function(k) {
if (Array.isArray(unsorted[k])) {
return {
[k]: unsorted[k].sort(function(a, b) {
return a.value - b.value
})
}
} else {
return {
[k]: unsorted[k]
};
}
});
Demo:
var unsorted = {
"2014": [{
"title": "Book",
"value": 10
},
{
"title": "Cheese",
"value": 2
}
],
"2015": [{
"title": "Movie",
"value": 9
}],
"2016": [{
"title": "Slay",
"value": 90
},
{
"title": "Back",
"value": 22
}
],
"selected_year": 2015
};
var sorted = Object.keys(unsorted).map(function(k) {
if (Array.isArray(unsorted[k])) {
return {
[k]: unsorted[k].sort(function(a, b) {
return a.value - b.value
})
}
} else {
return {
[k]: unsorted[k]
};
}
});
console.log(sorted);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216363a4617043.html
评论列表(0条)