javascript - 如何更改对象键值?
问题描述
如何仅更改对象键中的值?
例如:我想将 keyvalue 更新list_one
为任何值。
var ar = [
{
"list_one": [
{ id:1,product: 'Fortenza'}
]
},
{
"list_two": [
{ id:4,product:'SETLIQUI'}
]
}
]
我的代码:
var setTitleProduct = function(key="list_one",index=0) {
Object.keys(ar[index][key])[0] = 'new value'; //I know this doesnt work
console.log(ar[index][key]);
}
setTitleProduct("list_one",0)
解决方案
var ar = [
{
"list_one": [
{ id:1,product: 'Fortenza'}
]
},
{
"list_two": [
{ id:4,product:'SETLIQUI'}
]
}
]
ar[0]['someValue'] = ar[0]['list_one'];
delete ar[0]['list_one'];
console.log(ar);
推荐阅读
- ios - 使用计时器安排通知触发
- laravel-5 - Eloquent:使用 Model::where + 不显示数据
- string - 如何在 antlr 中捕获换行符并忽略 \n
- cordova - Cordova - 获取当前壁纸
- php - 隐藏某些用户在 Woocommerce 上发布的产品
- javascript - 如何将 cy.wrap() 属性值放入赛普拉斯测试中的类型字段
- php - 如何从 HTML 按钮执行 Python 脚本
- java - Java 通用比较器
- algorithm - Q: 代入的递归关系
- android - Android 从应用数据目录在 webview 中显示 img