gpt4 book ai didi

javascript - 如何将数组值添加到对象

转载 作者:行者123 更新时间:2023-11-28 17:23:15 25 4
gpt4 key购买 nike

我有一个值很少的数组。我想迭代数组并将这些值添加到对象中作为从具有 null 值的第二个对象元素开始的值。我不知道如何才能正确地做到这一点。这是我的代码

let objectParameters = {
"current_lang" : currentLang,
"project_name" : null,
"project_type" : null,
"min_price" : null,
"max_price" : null
}

let arrayValues = ["Project name", "Project Type", 150, 950];

arrayValues .forEach(function(item) {

//Add array value to an object
}

期望的输出

let objectParameters = {
"current_lang" : currentLang,
"project_name" : "Project name",
"project_type" : "Project Type",
"min_price" : 150,
"max_price" : 950
}

最佳答案

想出了这个:

let objectParameters = {
"current_lang" : "currentLang",
"project_name" : null,
"project_type" : null,
"min_price" : null,
"max_price" : null
};

let arrayValues = ["Project name", "Project Type", 150, 950],
keys = Object.keys(objectParameters);

keys.shift() // Removing the first key, which is not null

keys.forEach( (key,i) => objectParameters[key] = arrayValues[i])

console.log(objectParameters)

关于javascript - 如何将数组值添加到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52077488/

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