如何使用JS修改对象数组?

时间:2017-01-11 07:36:08

标签: javascript

我有包含数组对象的数据,而某些对象包含如下对象的数组:

原文:

[{
        "order": 1,
        "product": "A",
        "item": "A"
    }, {
        "order": 1,
        "product": "B",
        "item": "B"
    },

    {
        "order": 14,
        "product": "C",
        "item": "C",
        "lists": [{
            "order": 1,
            "product": "C1",
            "item": "c1"
        }, {
            "order": 1,
            "product": "c2",
            "item": "c3"
        }]
    }, {
        "order": 1,
        "product": "d",
        "item": "d"
    }, {
        "order": 72,
        "product": "e",
        "item": "e",
        "lists": [{
            "order": 2,
            "product": "e1",
            "item": "e1"
        }, {
            "order": 6,
            "product": "e2",
            "item": "e2"
        }]
    }, {
        "order": 1,
        "product": "e3",
        "item": "e3"
    }
]

我想更改数据,如下面的对象数组,

修改:

[{
    "order": 1,
    "product": "A",
    "item": "A"
}, {
    "order": 1,
    "product": "B",
    "item": "B"
}, {
    "order": 14,
    "product": "C",
    "item": "C"

}, {
    "order": 1,
    "product": "C1",
    "item": "c1"
}, {
    "order": 1,
    "product": "c2",
    "item": "c3"
}, {
    "order": 1,
    "product": "d",
    "item": "d"
}, {
    "order": 72,
    "product": "e",
    "item": "e"

}, {
    "order": 2,
    "product": "e1",
    "item": "e1"
}, {
    "order": 6,
    "product": "e2",
    "item": "e2"
}, {
    "order": 1,
    "product": "e3",
    "item": "e3"
}]

3 个答案:

答案 0 :(得分:1)

您可以使用Array#reduce并检查lists并使用实际值和lists项连接一个新数组,否则采用实际元素。

var data = [{ order: 1, product: "A", item: "A" }, { order: 1, product: "B", item: "B" }, { order: 14, product: "C", item: "C", lists: [{ order: 1, product: "C1", item: "c1" }, { order: 1, product: "c2", item: "c3" }] }, { order: 1, product: "d", item: "d" }, { order: 72, product: "e", item: "e", lists: [{ order: 2, product: "e1", item: "e1" }, { order: 6, product: "e2", item: "e2" }] }, { order: 1, product: "e3", item: "e3" }],
    flat = data.reduce(function (r, a) {
        return r.concat(a.lists && [{ order: a.order, product: a.product, item: a.item }].concat(a.lists) || a);
    }, []);

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

答案 1 :(得分:0)

解析数组,然后检查它是否有列表键,如果它已连接新数组中的列表并删除lists键并推送元素本身,否则直接推送元素。

var parsedData = [];
// Iterate through data array
data.forEach(function(el) {

  // If it doesnt have a list key directly push the element
  if (!el.hasOwnProperty("lists"))
    parsedData.push(el);

  // If it has a list key
  else {
    // Concat the list to the parsedData
    parsedData = parsedData.concat(el.lists);
    delete el.lists;
    // And push current item after deleting the list.
    parsedData.push(el);

  }

});

直播代码

var data = [{
    "order": 1,
    "product": "A",
    "item": "A"
  }, {
    "order": 1,
    "product": "B",
    "item": "B"
  },

  {
    "order": 14,
    "product": "C",
    "item": "C",
    "lists": [{
      "order": 1,
      "product": "C1",
      "item": "c1"
    }, {
      "order": 1,
      "product": "c2",
      "item": "c3"
    }]
  }, {
    "order": 1,
    "product": "d",
    "item": "d"
  }, {
    "order": 72,
    "product": "e",
    "item": "e",
    "lists": [{
      "order": 2,
      "product": "e1",
      "item": "e1"
    }, {
      "order": 6,
      "product": "e2",
      "item": "e2"
    }]
  }, {
    "order": 1,
    "product": "e3",
    "item": "e3"
  }
];

var parsedData = [];
// Iterate through data array
data.forEach(function(el) {

  // If it doesnt have a list key directly push the element
  if (!el.hasOwnProperty("lists"))
    parsedData.push(el);

  // If it has a list key
  else {
    // Concat the list to the parsedData
    parsedData = parsedData.concat(el.lists);
    delete el.lists;
    // And push current item after deleting the list.
    parsedData.push(el);

  }

});

document.write(JSON.stringify(parsedData));

答案 2 :(得分:0)

此代码段可能会对您有所帮助。



var data = [{
        "order": 1,
        "product": "A",
        "item": "A"
    }, {
        "order": 1,
        "product": "B",
        "item": "B"
    },

    {
        "order": 14,
        "product": "C",
        "item": "C",
        "lists": [{
            "order": 1,
            "product": "C1",
            "item": "c1"
        }, {
            "order": 1,
            "product": "c2",
            "item": "c3"
        }]
    }, {
        "order": 1,
        "product": "d",
        "item": "d"
    }, {
        "order": 72,
        "product": "e",
        "item": "e",
        "lists": [{
            "order": 2,
            "product": "e1",
            "item": "e1"
        }, {
            "order": 6,
            "product": "e2",
            "item": "e2"
        }]
    }, {
        "order": 1,
        "product": "e3",
        "item": "e3"
    }
];


var newObj = [];

data.forEach(function (val) {
  if(val.hasOwnProperty("lists")) {
    var lists = val.lists;
    
    delete val["lists"];
    
    newObj.push(val);
    
    lists.forEach(function (valList) {
      newObj.push(valList);
    });
  } else {
    newObj.push(val);
  }
});

console.log(JSON.stringify(newObj));