使用es6或lodash将字符串数组转换为对象数组 [英] Convert array of string to array of object using es6 or lodash
本文介绍了使用es6或lodash将字符串数组转换为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组,我想将其转换为对象数组.
I have an array of string which I want to turn it to array of object.
array = ['a', 'b', 'c'];
我想生成
array= [
{'name': 'a', 'isChecked': false, 'availibility': 0 },
{'name': 'b', 'isChecked': false, 'availibility': 0 },
{'name': 'b', 'isChecked': false, 'availibility': 0 }
];
我在下面尝试过,仍然返回了originalArray!
I tried below and still returning the originalArray!
array.map((name) => ({
name,
isChecked: false,
availability: 0
}));
您将如何执行此操作?
How would you do this?
推荐答案
您可以像这样使用map
:
array= ['a', 'b', 'c'];
let newArr = array.map(item => {
return {
'name': item,
'isChecked': false,
'availibility': 0
}
})
console.log(newArr);
这篇关于使用es6或lodash将字符串数组转换为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文