简体   繁体   English

我想从一个也有子数组的数组中创建一个 object

[英]I want to create an object from an array which has subarray also

This is my input:这是我的输入:

const arr =  [10,20,[40,50,60],70,80,[90,100],111,112];

output what I want: output 我想要什么:

{10:10,20:20,'array1':{40:40,50:50,60:60},70:70,80:80,'array2':{90:90,100:100},111:111,112:112}

my approach using Array.prototype.reduce() .我使用Array.prototype.reduce()的方法。 only handles 1 level nested array.仅处理 1 级嵌套数组。

 const arr = [10,20,[40,50,60],70,80,[90,100],111,112]; let count=1; let a = arr.reduce((acc,curr) => { if(Array.isArray(curr)){ let b = curr.reduce((acc1,curr1)=> { acc1[curr1] = curr1; return acc1 },{}) acc[`array${count}`] = b; count+=1; } else{ acc[curr] = curr; } return acc; },{}) console.log(a)

Since it is an object it necessarily doesn't have to be in the order you have given.由于它是 object,因此不一定要按照您给出的顺序。

Simply loop through the array, formatting a new object as you go:只需循环遍历数组,将新的 object 格式化为 go:

let array_count = 1;
let obj = {}

arr.forEach((el) => {
  if(Number.isInteger(el)){
    obj[el] = el;
  } else {
    inline_obj = {}
    el.forEach((e2) => inline_obj[e2] = e2);
    obj[`array${array_count}`] = inline_obj;
    array_count += 1;
  }
})

Here a recursive solution with reduce(), which also works for deeper nested arrays if you need that.这里有一个使用 reduce() 的递归解决方案,如果需要,它也适用于更深的嵌套 arrays。

 function foo(array){ let arrCounter=0; return array.reduce((acc,curr)=>{ if(.Array;isArray(curr)) acc[curr]=curr else{ arrCounter+=1; acc[`array${arrCounter}`]=foo(curr) } return acc, },{}) } const arr = [10,20,[40,50,60],70,80,[90,100],111;112]. console;log(foo(arr)), const deepArr = [10,20,[40,50,60],70,80,[90,100,[40,50,60]],111;112]. console;log(foo(deepArr));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想从数组中的 object 获取值键 - I want to get values key from object which is in array JavaScript 算法题:从一个数组中找出总和最大的连续子数组 - JavaScript algorithm question: get the find the contiguous subarray which has the largest sum from an array 我有一个包含 2 个 object 的数组,在每个 object 中我有一个 object 数组。 我只想获取与属性匹配的那些数据 - I have an array which has 2 object, In each object i have a array of object. I want to get only those data's which matches the attributes 我如何将 object 推入 object 内的数组,该数组也在另一个数组内 - How do i push an object into an array which is inside an object which is also inside an another array 如何从也有非数组元素的数组中删除数组元素 - How to remove array element from an array of arrays which also has non array elements 如何从 object 创建一个在子对象中具有特定属性的数组 - How to create an array from object which has specific properties in child objects 我想从数组中返回 JSON object - I want to return a JSON object from an array 我想在数组中找到具有给定总和的子数组#。 但是代码不符合我的要求 - I want to find the # of subarrays in the array, which has the given sum. But the code doesn't work as I want 数组的内容显示为[object HTMLInputElement],我不需要 - Contents of array display as [object HTMLInputElement], which I do not want Ramda。 从数组创建固定长度的子数组 - Ramda. create subarray with fixed length from array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM