如何将数组 object 转换为 two-dimensional 数组
How do I convert an array object into a two-dimensional array
这是一个数组列表。
const list = [
{
id: 5844,
option: 'fruit'
children: ['apple', 'banana', 'pear']
},
{
id: 5845,
option: 'vegetables'
children: ['tomato', 'potato', 'spinach']
}
]
我想得到一个这样的新数组
apple of fruit 的 children 是索引 0
蔬菜的番茄 children 是索引 = 0
所以他们匹配
[['apple', 'tomato'], ['banana', 'potato'], ['pear', 'spinach']]
我假设您的 children 长度相同。我们可以使用2个循环来对children.
的元素进行分组
首先循环迭代children的元素。
第二次循环迭代列表的元素。
这是解决您的问题的简单代码。
var listSize = list.length;
var childSize = list[0].children.length;
var expectedArrs = [];
for(var i=0;i<childSize;i++){
var groupByChild = [];
for(var j=0;j<listSize;j++){
groupByChild.push(list[j].children[i]);
}
expectedArrs.push(groupByChild);
}
console.log(expectedArrs);
控制台结果:
[["apple", "tomato"], ["banana", "potato"], ["pear", "spinach"]]
已解决:
const result = []
for(let i = 0; i< List[0].children; i++){
const result1 = []
result1.push(list[0].children[i] )
result1.push(list[1].children[i])
result.push(result1)
}
我想我们可以试试这段代码
const list = [
{
id: 5844,
option: 'fruit',
children: ['apple', 'banana', 'pear']
},
{
id: 5845,
option: 'vegetables',
children: ['tomato', 'potato', 'spinach']
}
]
var ans = []
list.forEach(item => {
item.children.forEach((child, index) => {
if (!ans[index]) {
ans[index] = []
ans[index].push(child)
} else {
ans[index].push(child)
}
})
})
var list = [
{
id: 5844,
option: 'fruit',
children: ['apple', 'banana', 'pear']
},
{
id: 5845,
option: 'vegetables',
children: ['tomato', 'potato', 'spinach']
}
]
var newArr = [];
var i=0;
while(i<list.length){
newArr.push(list[i].children);
i=i+1;
}
console.log(newArr)
使用此解决方案,数组中有多少对象并不重要。您可以 map
over the children in the first object and use it's length to return a flatMap
个子元素。
const list=[{id:5844,option:"fruit",children:["apple","banana","pear"]},{id:5845,option:"vegetables",children:["tomato","potato","spinach"]},{id:5846,option:"buildings",children:["church","warehouse","skyscraper"]}];
function getNewData(list) {
// `map` over the children in the first object
// using its index to return a new flattened array
// of all array object children
return list[0].children.map((_, i) => {
return list.flatMap(obj => obj.children[i]);
});
}
console.log(getNewData(list));
这是一个数组列表。
const list = [
{
id: 5844,
option: 'fruit'
children: ['apple', 'banana', 'pear']
},
{
id: 5845,
option: 'vegetables'
children: ['tomato', 'potato', 'spinach']
}
]
我想得到一个这样的新数组
apple of fruit 的 children 是索引 0
蔬菜的番茄 children 是索引 = 0
所以他们匹配
[['apple', 'tomato'], ['banana', 'potato'], ['pear', 'spinach']]
我假设您的 children 长度相同。我们可以使用2个循环来对children.
的元素进行分组首先循环迭代children的元素。
第二次循环迭代列表的元素。
这是解决您的问题的简单代码。
var listSize = list.length;
var childSize = list[0].children.length;
var expectedArrs = [];
for(var i=0;i<childSize;i++){
var groupByChild = [];
for(var j=0;j<listSize;j++){
groupByChild.push(list[j].children[i]);
}
expectedArrs.push(groupByChild);
}
console.log(expectedArrs);
控制台结果:
[["apple", "tomato"], ["banana", "potato"], ["pear", "spinach"]]
已解决:
const result = []
for(let i = 0; i< List[0].children; i++){
const result1 = []
result1.push(list[0].children[i] )
result1.push(list[1].children[i])
result.push(result1)
}
我想我们可以试试这段代码
const list = [
{
id: 5844,
option: 'fruit',
children: ['apple', 'banana', 'pear']
},
{
id: 5845,
option: 'vegetables',
children: ['tomato', 'potato', 'spinach']
}
]
var ans = []
list.forEach(item => {
item.children.forEach((child, index) => {
if (!ans[index]) {
ans[index] = []
ans[index].push(child)
} else {
ans[index].push(child)
}
})
})
var list = [
{
id: 5844,
option: 'fruit',
children: ['apple', 'banana', 'pear']
},
{
id: 5845,
option: 'vegetables',
children: ['tomato', 'potato', 'spinach']
}
]
var newArr = [];
var i=0;
while(i<list.length){
newArr.push(list[i].children);
i=i+1;
}
console.log(newArr)
使用此解决方案,数组中有多少对象并不重要。您可以 map
over the children in the first object and use it's length to return a flatMap
个子元素。
const list=[{id:5844,option:"fruit",children:["apple","banana","pear"]},{id:5845,option:"vegetables",children:["tomato","potato","spinach"]},{id:5846,option:"buildings",children:["church","warehouse","skyscraper"]}];
function getNewData(list) {
// `map` over the children in the first object
// using its index to return a new flattened array
// of all array object children
return list[0].children.map((_, i) => {
return list.flatMap(obj => obj.children[i]);
});
}
console.log(getNewData(list));