Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用array.map()从对象数组中查找元素_Javascript_Arrays_Ecmascript 6 - Fatal编程技术网

Javascript 使用array.map()从对象数组中查找元素

Javascript 使用array.map()从对象数组中查找元素,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,我有一个对象数组,我想返回一个包含所有ids的数组。例如: const arr = [ {Locations:[{id:2129, Name: 'testAA'}, {id:3431, Name: 'testAB'}, {id:4373, Name: 'testAC'}]}, {Locations:[{id:2545, Name: 'testBA'}, {id:3431, Name: 'testBB'}]} ]; 我想返回:[21293431437325453431]

我有一个对象数组,我想返回一个包含所有
id
s的数组。例如:

const arr = [
    {Locations:[{id:2129, Name: 'testAA'}, {id:3431, Name: 'testAB'}, {id:4373, Name: 'testAC'}]},
    {Locations:[{id:2545, Name: 'testBA'}, {id:3431, Name: 'testBB'}]}
];     
我想返回:
[21293431437325453431]

我试着做到以下几点:

arr.map((value) => {
    let newarray = [];
    return newarray += value['Locations'].map(ID => ID.id);
});
这将返回:
[“212934314373”、“25453431”]

如何组合这两个阵列

尝试使用

arr.map((值)=>{
设newarray=[];
返回newarray+=value['Locations'].map(ID=>ID.ID);
}).join(',).split(',).map((值)=>parseInt(值,10));
以下是“事件链”:
[“212934314373”,“254534431”]
->
“2129343143732545341”
->
[“2129”,“3431”,“4373”,“2545”,“3431”]
->
->[21293431437325453431]
尝试使用

arr.map((值)=>{
设newarray=[];
返回newarray+=value['Locations'].map(ID=>ID.ID);
}).join(',).split(',).map((值)=>parseInt(值,10));
以下是“事件链”:
[“212934314373”,“254534431”]
->
“2129343143732545341”
->
[“2129”,“3431”,“4373”,“2545”,“3431”]
->
[2129,3431,4373,2545,3431]
鉴于您的输入和输出不是一对一的映射,这似乎不是
.map()
的一个很好的用例。相反,我只考虑在内部数组中使用<代码> .MaP()/代码>,但是在外部.< /p>中使用<代码> .ReleDe()/<代码>。
const arr=[{Locations:[{id:2129,Name:'testAA'},{id:3431,Name:'testAB'},{id:4373,Name:'testAC'}]},{Locations:[{id:2545,Name:'testBA'},{id:3431,Name:'testBB'}];
const result=arr.reduce((acc,{Locations})=>[…acc,…Locations.map(i=>i.id)],[]);

控制台日志(结果)鉴于您的输入和输出不是一对一的映射,这似乎不是
.map()
的一个很好的用例。相反,我只考虑在内部数组中使用<代码> .MaP()/代码>,但是在外部.< /p>中使用<代码> .ReleDe()/<代码>。
const arr=[{Locations:[{id:2129,Name:'testAA'},{id:3431,Name:'testAB'},{id:4373,Name:'testAC'}]},{Locations:[{id:2545,Name:'testBA'},{id:3431,Name:'testBB'}];
const result=arr.reduce((acc,{Locations})=>[…acc,…Locations.map(i=>i.id)],[]);

控制台日志(结果)我喜欢泰勒·罗珀的答案

将两个阵列连接起来

可能
returnnewarray.concat(value['Locations'].map(ID=>ID.ID))没有测试

也可以运行类似的东西,这可能会更干净一些

arr.map((value) => {
    let newArray = [];
    Locations.map((location) => {
        return newArray.push(location.id)
    });
    if(newArray.length) return newArray;
    return null;
}

我喜欢泰勒·罗珀的回答

将两个阵列连接起来

可能
returnnewarray.concat(value['Locations'].map(ID=>ID.ID))没有测试

也可以运行类似的东西,这可能会更干净一些

arr.map((value) => {
    let newArray = [];
    Locations.map((location) => {
        return newArray.push(location.id)
    });
    if(newArray.length) return newArray;
    return null;
}
您可以尝试以下方法:

flatMap()
方法首先使用映射函数映射每个元素,然后将结果展平到新数组中。它与map()后跟深度为1的flat()相同,但是
flatMap()
通常非常有用,因为将两者合并到一个方法中会稍微更有效

map()

const arr=[
{位置:[{id:2129,名称:'testAA'},{id:3431,名称:'testAB'},{id:4373,名称:'testAC'}]},
{位置:[{id:2545,名称:'testBA'},{id:3431,名称:'testBB'}]}
];
const newarray=arr.flatMap(i=>i.Locations.map(j=>j.id));
log(newarray)您可以尝试:

flatMap()
方法首先使用映射函数映射每个元素,然后将结果展平到新数组中。它与map()后跟深度为1的flat()相同,但是
flatMap()
通常非常有用,因为将两者合并到一个方法中会稍微更有效

map()

const arr=[
{位置:[{id:2129,名称:'testAA'},{id:3431,名称:'testAB'},{id:4373,名称:'testAC'}]},
{位置:[{id:2545,名称:'testBA'},{id:3431,名称:'testBB'}]}
];
const newarray=arr.flatMap(i=>i.Locations.map(j=>j.id));

log(newarray)这里没有两个数组,而是一个由两个串联字符串组成的数组。您根本不需要
newarray[]
.map()
为您返回一个新数组

当您得到该值时,可以将其与
reduce()
组合使用

您想要的将如下所示:

arr
    .map((value) => value['Locations']
        .map(ID => ID.id))
    .reduce((accum, arr)=>accum.concat(arr));

这里没有两个数组,而是一个由两个串联字符串组成的数组。您根本不需要
newarray[]
.map()
为您返回一个新数组

当您得到该值时,可以将其与
reduce()
组合使用

您想要的将如下所示:

arr
    .map((value) => value['Locations']
        .map(ID => ID.id))
    .reduce((accum, arr)=>accum.concat(arr));
是此方案的正确工具,但如果浏览器兼容性禁止使用,则可以使用和展平:

const arr=[{Locations:[{id:2129,Name:'testAA'},{id:3431,Name:'testAB'},{id:4373,Name:'testAC'}]},{Locations:[{id:2545,Name:'testBA'},{id:3431,Name:'testBB'}];
log([].concat(…arr.map(e=>e.Locations.map(e=>e.id))是适用于此场景的正确工具,但如果浏览器兼容性禁止其使用,则可以使用和展平:

const arr=[{Locations:[{id:2129,Name:'testAA'},{id:3431,Name:'testAB'},{id:4373,Name:'testAC'}]},{Locations:[{id:2545,Name:'testBA'},{id:3431,Name:'testBB'}];

log([].concat(…arr.map(e=>e.Locations.map(e=>e.id))现在我看了,上面泰勒的答案更清晰更好-用那个代替。现在我看了,上面泰勒的答案更清晰更好-用那个代替。当累加器和子数组变平时,你