如何根据嵌套值从 json 文件中提取对象(使用 JavaScript)
How do I extract an object from a json file based on a nested value (using JavaScript)
const data = [
{0: {id: "1",cat:{string:[{color:"yellow",weight:"10"},{color:"orange",Weight:"10"}]}},
{1: {id: "1",cat:{string:[{color:"blue",weight:"10"},{color:"orange",Weight:"10"}]}},
{2: {id: "1",cat:{string:[{color:"white",weight:"10"},{color:"orange",Weight:"10"}]}},
{3: {id: "1",cat:{string:[{color:"blue",weight:"10"},{color:"orange",Weight:"10"}]}},
]
按颜色过滤:“黄色”
期望输出:[{0: {id: 1, country: "SA", address: "IOXX",cat:[{color: "yellow",weight: "10"}]}}]
您可以映射您的 data
数组并检查每个对象是否满足您的条件。
const data = [
{0: {id: 1, country: "SA", address: "IOXX",cat:[{color: "yellow",weight: "10"}]}},
{1: {id:2, country: "SAP", name: "N", address: "IOP",cat:[{color: "blue",weight: "10"}]}},
{2: {id:3, country: "S", name: "NO", address: "I",cat:[{color: "blue",weight: "10"}]}},
{3: {id:4, country: "SXX", name: "NOI", address: "INDIA",cat:[{color: "blue",weight: "12"}]}},
]
const filterByColor = function (color) {
const filteredCats = []
data.forEach((item, index) => {
const hasCorrectColor = item[index].cat.every((cat) => cat.color === color)
if (hasCorrectColor) {
filteredCats.push(item)
}
})
return filteredCats
}
filterByColor('yellow')
可以使用filter()
方法
const data = [
{ 0: { id: "1", cat: { string: [{ color: "yellow", weight: "10" }, { color: "orange", Weight: "10" }] } } },
{ 1: { id: "1", cat: { string: [{ color: "blue", weight: "10" }, { color: "orange", Weight: "10" }] } } },
{ 2: { id: "1", cat: { string: [{ color: "white", weight: "10" }, { color: "orange", Weight: "10" }] } } },
{ 3: { id: "1", cat: { string: [{ color: "blue", weight: "10" }, { color: "orange", Weight: "10" }] } } }
];
var res = data.filter((el, i) => data[i][i]['cat']['string'][0]['color'] === 'yellow');
console.log(res);
const data = [
{0: {id: "1",cat:{string:[{color:"yellow",weight:"10"},{color:"orange",Weight:"10"}]}},
{1: {id: "1",cat:{string:[{color:"blue",weight:"10"},{color:"orange",Weight:"10"}]}},
{2: {id: "1",cat:{string:[{color:"white",weight:"10"},{color:"orange",Weight:"10"}]}},
{3: {id: "1",cat:{string:[{color:"blue",weight:"10"},{color:"orange",Weight:"10"}]}},
]
按颜色过滤:“黄色”
期望输出:[{0: {id: 1, country: "SA", address: "IOXX",cat:[{color: "yellow",weight: "10"}]}}]
您可以映射您的 data
数组并检查每个对象是否满足您的条件。
const data = [
{0: {id: 1, country: "SA", address: "IOXX",cat:[{color: "yellow",weight: "10"}]}},
{1: {id:2, country: "SAP", name: "N", address: "IOP",cat:[{color: "blue",weight: "10"}]}},
{2: {id:3, country: "S", name: "NO", address: "I",cat:[{color: "blue",weight: "10"}]}},
{3: {id:4, country: "SXX", name: "NOI", address: "INDIA",cat:[{color: "blue",weight: "12"}]}},
]
const filterByColor = function (color) {
const filteredCats = []
data.forEach((item, index) => {
const hasCorrectColor = item[index].cat.every((cat) => cat.color === color)
if (hasCorrectColor) {
filteredCats.push(item)
}
})
return filteredCats
}
filterByColor('yellow')
可以使用filter()
方法
const data = [
{ 0: { id: "1", cat: { string: [{ color: "yellow", weight: "10" }, { color: "orange", Weight: "10" }] } } },
{ 1: { id: "1", cat: { string: [{ color: "blue", weight: "10" }, { color: "orange", Weight: "10" }] } } },
{ 2: { id: "1", cat: { string: [{ color: "white", weight: "10" }, { color: "orange", Weight: "10" }] } } },
{ 3: { id: "1", cat: { string: [{ color: "blue", weight: "10" }, { color: "orange", Weight: "10" }] } } }
];
var res = data.filter((el, i) => data[i][i]['cat']['string'][0]['color'] === 'yellow');
console.log(res);