[英]find an array based on its property array of object
im trying to get an array based on its element id of array of object and the structure is looks like this我试图根据 object 数组的元素 id 获取一个数组,结构看起来像这样
{"data": [
{
"id": 46,
"name": "shsjks",
"desc": "ehejej",
"code": "hshsbsb",
"activation_type": 1,
"detail": "[{\"id\": 413, \"name\": \"A\"}, {\"id\": 416, \"name\": \"B\"}]",
},
{
"id": 47,
"name": "hhksns",
"desc": "benemne",
"code": "gevewk",
"activation_type": 1,
"detail": "[{\"id\": 419, \"name\": \"C\"}, {\"id\": 423, \"name\": \"D\"}]"
},
]}
im trying to get the data based on the detail id and what i did我试图根据详细信息 id 和我做了什么来获取数据
let arr = a.data.filter(x => {
return (JSON.parse(x.detail).filter(x => x.id === 419))
});
// returned all instead of first element of the array
i want it to returned我希望它返回
// filter where id 419
{
"id": 47,
"name": "shjks",
"detail": "[{\"id\": 419, \"name\": \"C\"}, {\"id\": 423, \"name\": \"D\"}]"
....
}
.filter
will not perform a map. .filter
不会执行 map。 The return value of its callback function is supposed to just indicate whether the array element (from the top level array) should be included or not.其回调 function 的返回值应该只是指示是否应包含数组元素(来自顶级数组)。 So you should return a falsy value when you don't want an array to be included.因此,当您不希望包含数组时,您应该返回一个虚假值。 As .some
returns a boolean, that is a perfect candidate method to use for that purpose.由于.some
返回 boolean,这是用于该目的的完美候选方法。 And if you expect only one match, then .find
is more appropriate than .filter
:如果您希望只有一个匹配项,那么.find
比.filter
更合适:
let a = {"data": [{"id": 46,"name": "shsjks","desc": "ehejej","code": "hshsbsb","activation_type": 1,"detail": "[{\"id\": 413, \"name\": \"A\"}, {\"id\": 416, \"name\": \"B\"}]",},{"id": 47,"name": "hhksns","desc": "benemne","code": "gevewk","activation_type": 1,"detail": "[{\"id\": 419, \"name\": \"C\"}, {\"id\": 423, \"name\": \"D\"}]"},]} let result = a.data.find(x => JSON.parse(x.detail).some(x => x.id === 419)); console.log(result);
If you want the details
to remain parsed in the result, then first perform a map
:如果您希望details
在结果中保持解析,则首先执行map
:
let a = {"data": [{"id": 46,"name": "shsjks","desc": "ehejej","code": "hshsbsb","activation_type": 1,"detail": "[{\"id\": 413, \"name\": \"A\"}, {\"id\": 416, \"name\": \"B\"}]",},{"id": 47,"name": "hhksns","desc": "benemne","code": "gevewk","activation_type": 1,"detail": "[{\"id\": 419, \"name\": \"C\"}, {\"id\": 423, \"name\": \"D\"}]"},]} let result = a.data.map(x => ({...x, detail: JSON.parse(x.detail)})).find(x => x.detail.some(x => x.id === 419)); console.log(result);
If you are only interested in the id
key itself, then perform a .flatMap
to first collect all parsed detail
arrays, so you get one array with all the details, and then .find
the element you need:如果您只对id
键本身感兴趣,则执行.flatMap
以首先收集所有已解析的detail
arrays,这样您将获得一个包含所有详细信息的数组,然后.find
您需要的元素:
let a = {"data": [{"id": 46,"name": "shsjks","desc": "ehejej","code": "hshsbsb","activation_type": 1,"detail": "[{\"id\": 413, \"name\": \"A\"}, {\"id\": 416, \"name\": \"B\"}]",},{"id": 47,"name": "hhksns","desc": "benemne","code": "gevewk","activation_type": 1,"detail": "[{\"id\": 419, \"name\": \"C\"}, {\"id\": 423, \"name\": \"D\"}]"},]} let result = a.data.flatMap(x => JSON.parse(x.detail)).find(x => x.id === 419); console.log(result);
You should do something like this.你应该做这样的事情。
let arr = a.data.filter(x => {
return (JSON.parse(x.detail).filter(x => x.id === 419).length)
});
Array.filter returns []
if there is no value present.如果不存在值,则 Array.filter 返回[]
。 So when you return the array itself it is never considered as false.因此,当您返回数组本身时,它永远不会被视为错误。
The problem in your code is that the first filter need to return a boolean value (filter or not)您的代码中的问题是第一个过滤器需要返回 boolean 值(过滤器与否)
This will work for u:这对你有用:
const test = { data: [ { id: 46, name: 'shsjks', desc: 'ehejej', code: 'hshsbsb', activation_type: 1, detail: '[{"id": 413, "name": "A"}, {"id": 416, "name": "B"}]', }, { id: 47, name: 'hhksns', desc: 'benemne', code: 'gevewk', activation_type: 1, detail: '[{"id": 419, "name": "C"}, {"id": 423, "name": "D"}]', }, ], }; const arr = test.data.find((x) => JSON.parse(x.detail).some((d) => d.id === 419)); console.log(JSON.stringify(arr, null, 2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.