最好的转换方式是什么:
['a','b','c']
to:
{
0: 'a',
1: 'b',
2: 'c'
}
最好的转换方式是什么:
['a','b','c']
to:
{
0: 'a',
1: 'b',
2: 'c'
}
当前回答
最简单的方法如下:
const arr = ['a','b','c'];
let obj = {}
function ConvertArr(arr) {
if (typeof(arr) === 'array') {
Object.assign(obj, arr);
}
这样它只在数组中运行,然而,你可以用let全局对象变量或不带,这取决于你,如果你不带let,只运行object。加勒比海盗,分配({})。
其他回答
下面的方法将数组转换为具有特定给定键的对象。
/**
* Converts array to object
* @param {Array} array
* @param {string} key (optional)
*/
Array.prototype.ArrayToObject = function(key) {
const array = this;
const obj = {};
array.forEach((element, index) => {
if(!key) {
obj[index] = element;
} else if ((element && typeof element == 'object' && element[key])) {
obj[element[key]] = element;
}
});
return obj;
}
前任-
[{名称:“测试”},{名称:test1的}].ArrayToObject(“名字”);
会给
{test: {name: 'test'}, test1: {name: 'test1'}}
并且incase key没有作为参数提供给该方法
i.e. [{name: 'test'}, {name: 'test1'}].ArrayToObject();
会给
{0: {name: 'test'}, 1: {name: 'test1'}}
如果可以使用Map或Object。分配,很简单。
创建一个数组:
const languages = ['css', 'javascript', 'php', 'html'];
下面的代码创建了一个以index为键的对象:
Object.assign({}, languages)
在地图上复制上面相同的操作
转换为基于索引的对象{0:'css'}等…
const indexMap = new Map(languages.map((name, i) => [i, name] ));
indexMap.get(1) // javascript
转换为一个基于值的对象{css: 'css是伟大的'}等…
const valueMap = new Map(languages.map(name => [name, `${name} is great!`] ));
valueMap.get('css') // css is great
你可以使用累加器,也就是减少。
['a','b','c'].reduce(function(result, item, index, array) {
result[index] = item; //a, b, c
return result;
}, {}) //watch out the empty {}, which is passed as "result"
传递一个空对象{}作为起始点;然后递增地“增大”该对象。 在迭代结束时,结果将是{"0":"a", "1": "b", "2": "c"}
如果数组是键值对对象的集合:
[{ a: 1},{ b: 2},{ c: 3}].reduce(function(result, item) {
var key = Object.keys(item)[0]; //first property: a, b, c
result[key] = item[key];
return result;
}, {});
将产生:{a: 1, b: 2, c: 3}
为了完整起见,reduceRight允许你以相反的顺序遍历数组:
[{ a: 1},{ b: 2},{ c: 3}].reduceRight(/* same implementation as above */)
将产生:{c:3, b:2, a:1}
你的蓄能器可以为你的特定用途的任何类型。例如,为了交换数组中对象的键和值,传递[]:
[{ a: 1},{ b: 2},{ c: 3}].reduce(function(result, item, index) {
var key = Object.keys(item)[0]; //first property: a, b, c
var value = item[key];
var obj = {};
obj[value] = key;
result.push(obj);
return result;
}, []); //an empty array
将产生:[{1:"a"}, {2: "b"}, {3: "c"}]
与map不同,reduce不能用作1-1映射。您可以完全控制要包含或排除的项。因此,reduce可以实现过滤器的功能,这使得reduce非常通用:
[{ a: 1},{ b: 2},{ c: 3}].reduce(function(result, item, index) {
if(index !== 0) { //skip the first item
result.push(item);
}
return result;
}, []); //an empty array
将产生:[{2:"b"}, {3: "c"}]
警告:减少和对象。关键是ECMA第5版的一部分;你应该为不支持它们的浏览器(尤其是IE8)提供一个polyfill。
请参阅Mozilla的默认实现。
这不是直接相关的,但我来这里寻找一个合并嵌套对象如一行
const nodes = {
node1: {
interfaces: {if1: {}, if2: {}}
},
node2: {
interfaces: {if3: {}, if4: {}}
},
node3: {
interfaces: {if5: {}, if6: {}}
},
}
解决方案是结合使用reduce和对象扩展:
const allInterfaces = nodes => Object.keys(nodes).reduce((res, key) => ({...res, ...nodes[key].interfaces}), {})
使用javascript#forEach可以做到这一点
var result = {},
attributes = ['a', 'b','c'];
attributes.forEach(function(prop,index) {
result[index] = prop;
});
ECMA6:
attributes.forEach((prop,index)=>result[index] = prop);