我有以下对象数组:
[
{
"messages": {
"m1": {
"message": "Relay malfunction found. Cause: moth.",
"sender": "ghopper"
}
},
"title": "Historical Tech Pioneers",
"key": "one"
} ]
但我希望将其转换为数组数组。例如:我想打开下面的标题字段:
"title": "Historical Tech Pioneers"
进入:
"title": [ "Historical Tech Pioneers" ]
感谢您的帮助
答案 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]);
});