[英]Sort nested json complex array
I search how to sort by place.city
this kind of object who have id's for keys. 我搜索如何按place.city
排序,这种具有ID的键的对象。 The need is to keep id's for first keys… 需要保留第一把钥匙的ID…
{
"123": {
"place": {
"city": "New York",
"country": "USA"
},
"person": {
"name": "Bob",
"age": 45
}
},
"456": {
"place": {
"city": "Chicago",
"country": "USA"
},
"person": {
"name": "Louis",
"age": 34
}
},
"789": {
"place": {
"city": "Dallas",
"country": "USA"
},
"person": {
"name": "Kevin",
"age": 27
}
}
}
I try some kind of function like this and the expected result is not here. 我尝试了这种功能,但预期结果不在这里。
let result = _(myObject).map(function (value, key) {
return _.defaults({ name: key }, value)
}).sortBy('city').value()
You can't sort an object.. You can, however, convert your object to an array and sort that. 您不能对对象进行排序。但是,您可以将对象转换为数组并对其进行排序。
var data ={ "123" : { "place": { "city": "New York", "country": "USA" }, "person": { "name": "Bob", "age": 45 } }, "456" : { "place": { "city": "Chicago", "country": "USA" }, "person": { "name": "Louis", "age": 34 } }, "789" : { "place": { "city": "Dallas", "country": "USA" }, "person": { "name": "Kevin", "age": 27 } } }; var sortedByPlace = _.sortBy(Object.keys(data).map(k => ({id:k, ...data[k]})), (d)=> d.place.city) console.log(sortedByPlace);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
It is not possible to sort an object, you need to make it a list. 无法对对象进行排序,需要将其列出。
import { map, flow } from 'lodash'
import { sortBy } from 'lodash/fp'
cities => flow(
map(places, (place, id) => { id, ...place }),
sortBy('city'),
)()
Your second question begs the question (mh...) if you want local sort. 如果要进行本地排序,第二个问题就是问题(mh ...)。 That would be 那会是
import { mapValues } from 'lodash'
import { sortBy } from 'lodash/fp'
data => mapValues(data, sortBy('place.city'))
If you look at this answer, the following should work 如果您查看此答案,则以下方法应该有效
var sort = function (prop, arr) {
prop = prop.split('.');
var len = prop.length;
arr.sort(function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return arr;
};
sort("place.city", myObject);
You cant sort an object. 您无法对对象进行排序。 However you could create a sorted array that contains references to the objects: 但是,您可以创建一个包含对象引用的排序数组:
const sorted = Object.values(myObject).sort((a, b) => a.place.city.localeCompare(b.place.city));
I realize the object I have to treat (get from an obscur API) is a little bit more complicated than my first example :/ 我意识到我必须处理的对象(从模糊的API获取)比我的第一个示例稍微复杂:/
So the responses you nicely give are not working anymore… 因此,您很好的答复不再起作用了……
Do you see the subtlety on this new object ? 您看到这个新物体的微妙之处了吗?
The point stay to sort by place.city
要点依place.city
排序
{
"123": {
0: {
"place": {
"city": "New York",
"country": "USA"
},
"person": {
"name": "Bob",
"age": 45
}
},
1: {
"place": {
"city": "New York",
"country": "USA"
},
"person": {
"name": "James",
"age": 32
}
}
},
"456": {
0: {
"place": {
"city": "Chicago",
"country": "USA"
},
"person": {
"name": "Louis",
"age": 34
}
},
1: {
"place": {
"city": "Chicago",
"country": "USA"
},
"person": {
"name": "Christine",
"age": 65
}
}
},
"789": {
0: {
"place": {
"city": "Dallas",
"country": "USA"
},
"person": {
"name": "Kevin",
"age": 27
}
},
1: {
"place": {
"city": "Dallas",
"country": "USA"
},
"person": {
"name": "Robert",
"age": 55
}
},
2: {
"place": {
"city": "Dallas",
"country": "USA"
},
"person": {
"name": "Danny",
"age": 62
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.