2015-10-14 245 views
7

我需要將對象數組轉換爲對象。我已經完成了以下邏輯。有沒有最好的方法來處理這個問題?將對象數組轉換爲對象的最佳方法?

Fiddler Version

var before = [{ 
 
    "x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"] 
 
}, { 
 
    "CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}]; 
 

 
var after = {}; 
 

 
for (var i = 0; i < before.length; i++) { 
 
    var keys = Object.keys(before[i]); 
 

 
    after[keys] = before[i][keys]; 
 
} 
 

 
console.log(after) 
 
document.writeln(JSON.stringify(after))

+0

你的版本是不正確的,如果'{A:1,B:4,...}'對象關鍵將不止一個。 –

+0

您的方法僅適用於內部數組每個只有一個鍵。多於一個就會失敗。你需要第二個循環。 – Yoshi

回答

6

您可以使用forEach遍歷數組,你忘了,如果裏面有多種元素迭代嵌套陣列上方的一個對象。

所以,有問題的代碼不會爲

var arr = [{'a': 'b', 'c': 'd'}]; 

演示

var before = [{ 
 
    "x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"] 
 
}, { 
 
    "CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}]; 
 

 
var after = {}; 
 

 
before.forEach(function(obj) { 
 
    // obj here is the element of the array, i.e. object 
 

 
    // Looping over all the keys of the object 
 
    Object.keys(obj).forEach(function(key) { 
 
    // key here is the key of the object 
 
    after[key] = obj[key]; 
 
    }); 
 
}); 
 

 
console.log(after); 
 
document.writeln('<pre>' + JSON.stringify(after, 0, 2) + '</pre>');

+0

謝謝你的回覆。我還需要將日期格式從字符串更改爲日期類型。我更新了代碼片段。我可以如何有效地轉換? –

+1

@PremkumarJayaseelan請對日期格式更改提出不同的問題 – Tushar

+0

嗨,兄弟,感謝'JSON.stringify(after,0,2)'。我不知道。你怎麼看我的答案。你能告訴我(這是否好方法)? –

3

我們用最好的函數式編程的JavaScript中:

使用reduce我們不需要臨時變量添加到外部範圍。

for (var i in obj)一般快於Obj.keys(obj).forEachhttps://jsperf.com/for-in-versus-object-keys-foreach

var before = [{ 
 
    "x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"] 
 
}, { 
 
    "CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}]; 
 
var after = before.reduce(function(dst, src){ 
 
    // import every key: val in src into dst 
 
    // and keep doing it for every element in the array. 
 
    for (var key in src) 
 
    if ({}.hasOwnProperty.call(src, key)) 
 
     dst[key] = src[key]; 
 
    return dst; 
 
}, {}); // start with an empty object to avoid modifying items in before array. 
 

 
document.writeln("<pre>" + JSON.stringify(after, null, 4) + "</pre>")

+0

僅有代碼的答案不是最優的。你應該解釋爲什麼你的代碼符合OP的要求。 – RobG

1

如何Object.assign工作?

var before = [{ 
 
    "x": ["2015-10-14T01:59:59.999+05:30", "2015-10-14T03:59:59.998+05:30", "2015-10-14T05:59:59.997+05:30", "2015-10-14T07:59:59.996+05:30", "2015-10-14T09:59:59.995+05:30", "2015-10-14T11:59:59.994+05:30", "2015-10-14T13:59:59.993+05:30", "2015-10-14T15:59:59.992+05:30", "2015-10-14T17:59:59.991+05:30", "2015-10-14T19:59:59.990+05:30", "2015-10-14T21:59:59.989+05:30", "2015-10-14T23:59:59.988+05:30"] 
 
}, { 
 
    "CleanCoal": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Middelings": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "Prime": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}, { 
 
    "SpiralProd": ["0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00"] 
 
}]; 
 

 
var after = {}; 
 

 
before.map(function(i){ 
 
    return Object.assign(after,i); 
 
}) 
 

 

 
console.log(after) 
 
document.writeln(JSON.stringify(after))

3

在最新的瀏覽器[5的ECMAScript],我們有reduce它返回一個對象下

var before = [{"x":["2015-10-14T01:59:59.999+05:30","2015-10-14T03:59:59.998+05:30","2015-10-14T05:59:59.997+05:30","2015-10-14T07:59:59.996+05:30","2015-10-14T09:59:59.995+05:30","2015-10-14T11:59:59.994+05:30","2015-10-14T13:59:59.993+05:30","2015-10-14T15:59:59.992+05:30","2015-10-14T17:59:59.991+05:30","2015-10-14T19:59:59.990+05:30","2015-10-14T21:59:59.989+05:30","2015-10-14T23:59:59.988+05:30"]},{"CleanCoal":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]},{"Middelings":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]},{"Prime":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]},{"SpiralProd":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]}]; 
 

 

 
var object = before.reduce(function(o, v, i) { 
 
    o[i] = v; 
 
    return o; 
 
}, {}); 
 

 
console.log(object);

試小提琴這裏http://jsfiddle.net/pscytrgj/

+1

* reduce *不一定會返回一個對象,它將返回任何回調作爲累加器返回的值(可以是任何值)。 MDN提供了[polyfill for * reduce *](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Polyfill),因此也適應了舊版瀏覽器。 – RobG

+0

@RobG是的,這是真的!有點爲我學習以及:)謝謝! –

2

你可以做到這一點通過JSON.stringify()

var before = [{ 
 
    "x": ["1", "2"], 
 
    y: { 
 
    a: 3 
 
    } 
 
}, { 
 
    "CleanCoal": ["0.00", "0.00"], 
 
    b: 4 
 
}, { 
 
    "Middelings": ["0.00", "0.00"] 
 
}, { 
 
    "Prime": ["0.00", "0.00", "0.00"] 
 
}, { 
 
    "SpiralProd": ["0.00", "0.00", "0.00"] 
 
}]; 
 

 
var after = {}; 
 
var b = "", 
 
    i = -1; 
 
while (before[++i]) { 
 
    var str = JSON.stringify(before[i]); 
 
    b += str.slice(1, str.length - 1); 
 
    if (before[i + 1]) b += ","; 
 
} 
 
after = JSON.parse("{" + b + "}"); 
 
console.log(after); 
 
document.write("<pre>" + JSON.stringify(after, 0, 3) + "</pre>")

相關問題