gpt4 book ai didi

javascript - 无需手动遍历每个对象即可将属性添加到大量对象

转载 作者:行者123 更新时间:2023-11-30 09:26:02 25 4
gpt4 key购买 nike

export const Schools = [
{
value: "Alabama A & M University"
},
{
value: "University of Alabama at Birmingham"
},
{
value: "Amridge University"
},
{
value: "University of Alabama in Huntsville"
},
{
value: "Alabama State University"
},
{
value: "University of Alabama System Office"
},
{
value: "The University of Alabama"
}, +7000 more schools ]

我需要其中的每一个都有一个键和一个名为文本的字段。文本应该等于值,键应该是唯一的。

如果我的数据文件中没有实际手动遍历 7000 所学校,我该如何做到这一点?

例如:

 {
value: "University of Alabama in Huntsville",
text: "University of Alabama in Huntsville",
key: 9,
},

最佳答案

导入后可以在旧数组上应用map运算符创建新数​​组

var Schools = [{
value: "Alabama A & M University"
},
{
value: "University of Alabama at Birmingham"
},
{
value: "Amridge University"
},
{
value: "University of Alabama in Huntsville"
},
{
value: "Alabama State University"
},
{
value: "University of Alabama System Office"
},
{
value: "The University of Alabama"
}
]
var newVals = Schools.map(function(item, index) {

return {
value: item.value,
text: item.value,
key: index,
}
});

console.log(newVals)

关于javascript - 无需手动遍历每个对象即可将属性添加到大量对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49091635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com