转换成字符串在Javascript角6阵列

问题描述 投票:-4回答:3

我具有从console.log(temp)所示的动态生成的阵列:

[{user_id: "[email protected]", status: "Idle"}], 
[{user_id: "[email protected]", status: "Busy"}]

我的代码:

this.scheduleService.getShiftSchedule().subscribe((temp)=>{
  this.api = temp;
  console.log(temp);
})

如何更改我的数组的格式变成像下面的阵列?

[[{id: 1,content: '[email protected]'},{id: 2,content: 'Idle'}],
[{id: 1,content: '[email protected]'},{id: 2,content: 'Busy'}]]
javascript arrays angular multidimensional-array
3个回答
2
投票

你可以通过采取通缉键及其id的数组对象映射。

var data = [{ user_id: "[email protected]", status: "Idle" }, { user_id: "[email protected]", status: "Busy" }],
    ids = [['user_id', 1], ['status', 2]],
    result = data.map(o => ids.map(([key, id]) => ({ id, content: o[key] })));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0
投票

VAR =阵列JSON.parse( “[” +串+ “]”);


0
投票

是ARR = [1,2,3,4,5,6,7,8,9]

var newArr = [];
while(arr.length) newArr.push(arr.splice(0,3));

console.log(newArr)

你可以尝试这样的

© www.soinside.com 2019 - 2024. All rights reserved.