我需要帮助将这个对象数组转换为数组数组

时间:2018-01-23 14:07:18

标签: javascript

我有以下对象数组:

    [
{
    "messages": {
        "m1": {
            "message": "Relay malfunction found. Cause: moth.",
            "sender": "ghopper"
        }
    },
    "title": "Historical Tech Pioneers",
    "key": "one"
} ]

但我希望将其转换为数组数组。例如:我想打开下面的标题字段:

    "title": "Historical Tech Pioneers"

进入:

    "title": [ "Historical Tech Pioneers" ]

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

假设声明:

var myArr = [
  {
    "messages": {
      "m1": {
        "message": "Relay malfunction found. Cause: moth.",
        "sender": "ghopper"
      }
    },
    "title": "Historical Tech Pioneers",
    "key": "one"
  }
];

您可以迭代每个元素并使用以下命令重新分配属性:

myArr.forEach(o => o.title = [o.title]);

或没有ES6:

myArr.forEach(function (o) {
  o.title = [o.title]);
});
相关问题