简体   繁体   中英

How to change Format to array in javascript?

I am getting some value in this format

 [["first"],["second"],["third"]]

and i want to get

 {"first","second","third"}

what should i do in javascript?

This is illegal: {"first","second","third"}

An object must be a mapping of key-value pairs. The best you can achieve is this:

{`first`: null, `second`: null, `third`: null}

JSFiddle DEMO

var arr2d = [["first"], ["second"], ["third"]];
var arrFlat = arr2d.reduce(function(a, b) {
    return a.concat(b);
});

var obj = {};
for (var i = 0; i < arrFlat.length; i++) {
    obj[arrFlat[i]] = null;
}

console.log(obj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM