var arr=[1,2,3]转换为[{id:1},{id:2},{id:3}]
时间: 2024-05-01 11:24:05 浏览: 22
可以使用数组的 `map` 方法来实现:
```
const arr = [1, 2, 3];
const result = arr.map((item) => {
return { id: item };
});
console.log(result); // [{ id: 1 }, { id: 2 }, { id: 3 }]
```
在上面的代码中,`map` 方法会遍历数组 `arr` 中的每一个元素,并将其传入回调函数中进行处理。回调函数返回一个包含 `id` 属性的对象,最终将所有处理后的对象组成一个新的数组。
相关问题
var arr = [ { date: "5月15号", id: 1 }, { date: "5月15号", id: 2 }, { date: "5月15号", id: 3 }, { date: "5月15号", id: 4 }, { date: "5月18号", id: 5 }, { date: "5月18号", id: 6 }, { date: "5月18号", id: 7 }, { date: "5月18号", id: 8 }, { date: "4月1号", id: 9 }, { date: "4月1号", id: 10 }, { date: "4月1号", id: 11 }, ]要转成// var obj = { // "5月15号" : [ { date:"5月15号" , id:1 }, // { date:"5月15号" , id:2 }, // { date:"5月15号" , id:3 }, // { date:"5月15号" , id:4 },], // "5月18号":[ { date:"5月18号" , id:5 }, // { date:"5月18号" , id:6 }, // { date:"5月18号" , id:7 }, // { date:"5月18号" , id:8 },], // "4月1号":[ { date:"4月1号" , id:9 }, // { date:"4月1号" , id:10 }, // { date:"4月1号" , id:11 }] // }
可以使用 reduce 方法将数组转换成对象:
```
var arr = [
{ date: "5月15号", id: 1 },
{ date: "5月15号", id: 2 },
{ date: "5月15号", id: 3 },
{ date: "5月15号", id: 4 },
{ date: "5月18号", id: 5 },
{ date: "5月18号", id: 6 },
{ date: "5月18号", id: 7 },
{ date: "5月18号", id: 8 },
{ date: "4月1号", id: 9 },
{ date: "4月1号", id: 10 },
{ date: "4月1号", id: 11 },
];
var obj = arr.reduce(function(prev, item) {
if (!prev[item.date]) {
prev[item.date] = [];
}
prev[item.date].push(item);
return prev;
}, {});
console.log(obj);
```
打平的数组换成树形数组:输入:[ ( id: 1, parentld:null} ,( id: 2, parentid:1},{id:3,parentid:2}]输出:[ ( id: 1, children:} ,( id: 2, children:[{id:3,2}]
抱歉,我可以回答这个问题。可以通过以下方法将打平的数组转换为树形数组:
```
function generateTree(arr, parentid) {
var tree = [];
for(var i=0; i<arr.length; i++) {
if(arr[i].parentid === parentid) {
var node = {
id: arr[i].id,
children: generateTree(arr, arr[i].id)
};
tree.push(node);
}
}
return tree;
}
var arr = [
{id: 1, parentid: null},
{id: 2, parentid: 1},
{id: 3, parentid: 2}
];
generateTree(arr, null);
```
输出结果为:
```
[
{
id: 1,
children: [
{
id: 2,
children: [
{
id: 3,
children: []
}
]
}
]
}
]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)