如何将两个数组放入新数组

时间:2018-11-16 08:46:13

标签: javascript arrays arraylist

请查看我现在拥有的结果:

0: {
  tasks: 
    0: {CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: "Category 2"}
    1: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"}
    2: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"}
} 
1: {
  tasks: 
    0: {CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: 
      "Category 2"}
    1: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: 
      "Category 1"}
    2: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: 
    "Category 1"}
}
2: {
    tasks: 
      0: {CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: 
      "Category 2"}
      1: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: 
      "Category 1"}
 }

以上是我当前正在循环的结果,这意味着总共3个结果正在循环。

在每个结果中,您都可以看到任务存在,并且在任务中还包含循环中的数据。

现在,我要合并所有任务数据并放入新数组中。

我想要以下结果:

0: { 
  CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: "Category 2"
  CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"
  CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"
}
1: { 
  CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: "Category 2"
  CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"
  CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"}   
2: {
  CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: "Category 2"
  CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"
}

5 个答案:

答案 0 :(得分:1)

我不认为您可以得到想要的结果(对象中不能有两个具有相同名称的键,例如:CategoryId

  

0:{     CategoryId:“ 4a6dcb18-94f6-4ef5-8ea8-9872cce5af92”,CategoryName:“类别2”,     CategoryId:“ ee3102c7-de53-48f7-90ca-b5c3f43f941c”,CategoryName:“类别1”,     CategoryId:“ ee3102c7-de53-48f7-90ca-b5c3f43f941c”,CategoryName:“类别1”   }

您可以采取的替代措施是, 我假设您的数组是obj,并且您正在像这样循环它,

var newArr = []
obj.forEach((ele,idx)=>{
    newArr.push(ele["tasks"])
})

console.log(newArr)

并获得

0 : [{ CategoryId: '4a6dcb18-94f6-4ef5-8ea8-9872cce5af92', CategoryName: 'Category 2' }, { CategoryId: 'ee3102c7-de53-48f7-90ca-b5c3f43f941c', CategoryName: 'Category 1' }, { CategoryId: 'ee3102c7-de53-48f7-90ca-b5c3f43f941c', CategoryName: 'Category 1' }] 1 : [{ CategoryId: '4a6dcb18-94f6-4ef5-8ea8-9872cce5af92', CategoryName: 'Category 2' }, { CategoryId: 'ee3102c7-de53-48f7-90ca-b5c3f43f941c', CategoryName: 'Category 1' }, { CategoryId: 'ee3102c7-de53-48f7-90ca-b5c3f43f941c', CategoryName: 'Category 1' }] 2 : [{ CategoryId: '4a6dcb18-94f6-4ef5-8ea8-9872cce5af92', CategoryName: 'Category 2' }, { CategoryId: 'ee3102c7-de53-48f7-90ca-b5c3f43f941c', CategoryName: 'Category 1' }]

var json = `[{
  "tasks": 
    [{"CategoryId": "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", "CategoryName": "Category 2"},
    {"CategoryId": "ee3102c7-de53-48f7-90ca-b5c3f43f941c", "CategoryName": "Category 1"},
    {"CategoryId": "ee3102c7-de53-48f7-90ca-b5c3f43f941c", "CategoryName": "Category 1"}]
}, 
{
  "tasks": 
    [{"CategoryId": "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", "CategoryName":"Category 2"},
    {"CategoryId": "ee3102c7-de53-48f7-90ca-b5c3f43f941c", "CategoryName": "Category 1"},
    {"CategoryId": "ee3102c7-de53-48f7-90ca-b5c3f43f941c", "CategoryName":"Category 1"}]
},
{
"tasks":
      [{"CategoryId": "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", "CategoryName": "Category 2"},
      {"CategoryId": "ee3102c7-de53-48f7-90ca-b5c3f43f941c", "CategoryName": "Category 1"}]
}]`


var obj = JSON.parse(json)

var newArr = []
obj.forEach((ele,idx)=>{
  newArr.push(ele["tasks"])
})

newArr.forEach((element,idx)=>{

  console.log(idx,element)

})

答案 1 :(得分:0)

您在此处发布的这些格式正确的对象数据是否正确?还是可以从循环中获取格式正确的数据?

   var result_field = { 0: {
0: {CategoryId: "4a6dcb18-94f6-4ef5-8ea8-9872cce5af92", CategoryName: "Category 2"},
1: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"},
2: {CategoryId: "ee3102c7-de53-48f7-90ca-b5c3f43f941c", CategoryName: "Category 1"},

}}

如果是,则可以使用每个循环在其中查找结果来创建另一个对象变量。

each(result_field[0] , function( index, value ){ 
var new_field = //formate as per your requirement 
}

答案 2 :(得分:0)

您可以为此使用map

const data = [{"tasks":[{"CategoryId":"4a6dcb18-94f6-4ef5-8ea8-9872cce5af92","CategoryName":"Category 2"},{"CategoryId":"ee3102c7-de53-48f7-90ca-b5c3f43f941c","CategoryName":"Category 1"},{"CategoryId":"ee3102c7-de53-48f7-90ca-b5c3f43f941c","CategoryName":"Category 1"}]},{"tasks":[{"CategoryId":"4a6dcb18-94f6-4ef5-8ea8-9872cce5af92","CategoryName":"Category 2"},{"CategoryId":"ee3102c7-de53-48f7-90ca-b5c3f43f941c","CategoryName":"Category 1"},{"CategoryId":"ee3102c7-de53-48f7-90ca-b5c3f43f941c","CategoryName":"Category 1"}]},{"tasks":[{"CategoryId":"4a6dcb18-94f6-4ef5-8ea8-9872cce5af92","CategoryName":"Category 2"},{"CategoryId":"ee3102c7-de53-48f7-90ca-b5c3f43f941c","CategoryName":"Category 1"}]}];

console.log(
  data.map((item) => item.tasks)
);

答案 3 :(得分:0)

以下是我想显示的示例设置:

    // These are the objects containing your data of each Category
    var data1 = {CategoryName: "Category 2", CategoryColor: "purple"};
    var data2 = {CategoryName: "Category 5", CategoryColor: "yellow"};
    var data3 = {CategoryName: "Category 2", CategoryColor: "blue"};

    // These are the arrays holding your category objects
    var array1 = [data1, data2];
    var array2 = [data1, data2];
    var array3 = [data3, data1];

    // The tasks property in the objects contain the arrays with the catergory objects in.
    var obj1 = {timeKey: "Wednesday", timeAgo: "2 days ago", tasks: array1};
    var obj2 = {timeKey: "Tuesday", timeAgo: "3 days ago", tasks: array2};
    var obj3 = {timeKey: "Monday", timeAgo: "4 days ago", tasks: array3};

    // The array displayed in your console
    var myArray = [obj1, obj2, obj3];

要将任务属性值放入新数组中,只需向下移至要检索数据的对象中即可。

var newData = myArray[1]["tasks"] // this will select the array containing the Category objects.

var newArray = [];
newArray.push(newData);

如果您只想从数组中选择一种“对象”类别,请进一步:

var newData = myArray[1]["tasks"][0];
var newArray = [];
newArray.push(newData);

在控制台中对此进行测试,以浏览 myArray 阵列。

答案 4 :(得分:0)

我自己找到了解决方法:

data.forEach(elementData => {
    if(elementData.tasks) {
      arrayFirst.push(elementData.tasks);
    }
  });
  arrayFirst.forEach(dataNext=>{
    if(dataNext) {
      dataNext.forEach(elementNext => {
        if(elementNext) {
          newArrayData.push(elementNext);
        }
      });
    }
  })

console.log(newArrayData)