gpt4 book ai didi

javascript - 如何更新对象数组中的键值?

转载 作者:行者123 更新时间:2023-12-01 01:58:07 32 4
gpt4 key购买 nike

这是我到目前为止所拥有的:

var onepage = [{
name: 'Custom Design',
hours: 5
},{
name: 'Premium Theme',
hours: 2
},{
name: 'SEO',
hours: 5
},{
name: 'Backlink Building',
hours: 6
},{
name: 'Article Writing',
hours: 7
},{
name: 'Copywriting',
hours: 3
},{
name: 'Development',
hours: 8
}
]

window.onkeyup = keyup;

var numberofPages;

function keyup(e) {
numberofPages = e.target.value;
newHours = [];

for (var i = 0; i < onepage.length; i++) {
var totalHours = numberofPages * onepage[i].hours;
newHours.push(totalHours);
}
console.log(newHours);
}
<input type="text" placeholder="Number of Pages" id="pages" value="1">

用户输入页数,然后将其乘以小时并插入数组newHours,从那里我想更新原始对象数组小时。

我不确定如何更新它,或者创建新的对象数组是否有意义?

最佳答案

您可以直接更新数组,而无需创建 newArray。请注意,直接更新原始数组的小时将导致不期望的结果。因此,添加了一个新属性calculatedHours

var onepage = [{name: 'Custom Design',hours: 5},{name: 'Premium Theme',hours: 2},{name: 'SEO',hours: 5},{name: 'Backlink Building',hours: 6},{name: 'Article Writing',hours: 7},{name: 'Copywriting',hours: 3},{name: 'Development',hours: 8}];

window.onkeyup = keyup;

var numberofPages;

function keyup(e) {
numberofPages = e.target.value;
newHours = [];

for (var i = 0; i < onepage.length; i++) {
onepage[i].calculatedHours = numberofPages * onepage[i].hours;
}
console.log(onepage);
}
<input type="text" placeholder="Number of Pages" id="pages" value="1">

关于javascript - 如何更新对象数组中的键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50842094/

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