[英]How to add index values of array in object to a key
我在变量info
有一个对象:
0: {ProId: "Space", Name: "cake", Quantity: 1}
1: {ProId: "new", Name: "walk", Quantity: 1}
我正在尝试将 Quantity 值更改为每个索引的index+1
,
我试图有一个静态值:
for (var key in obj){
var value= obj[key];
value['Quantity'] = 5;
}
预期 O/P:
console.log(info)
0: {ProId: "Space", Name: "cake", Quantity: 1}
1: {ProId: "new", Name: "walk", Quantity: 2}
2: {............................Quantity: 3}.....
但是我可以知道如何获得上述Quantity
值吗?
一些提示:
toString
应用到它。为了获得一个属性的新值,你可以改变给定的数据
使用:
Array#forEach
用于访问数组的所有项目, var array = [{ ProId: "Space", Name: "cake", Quantity: "1" }, { ProId: "new", Name: "walk", Quantity: "1" }]; array.forEach((object, i) => object.Quantity = i + 1); console.log(array);
或者通过映射数组来获取新对象。
使用:
var array = [{ ProId: "Space", Name: "cake", Quantity: "1" }, { ProId: "new", Name: "walk", Quantity: "1" }], newArray = array.map((object, i) => ({ ...object, Quantity: i + 1 })); console.log(newArray);
您可以映射数组并返回一个新数组,但将旧对象与具有更新的Quantity
值的新对象合并,例如:
const data = [{ProId: "Space", Name: "cake", Quantity: "1"}, {ProId: "new", Name: "walk", Quantity: "1"}], updatedData = data.map((x, i)=> ({...x, Quantity: ++i})); console.log(updatedData)
.as-console-wrapper { max-height: 100% !important; top: 0; }
更新对象键值的简单示例(在map()
方法中完成)如下:
let obj = { a: 0, b: 0}; let newObj = { ...obj, a: 1 }; console.log( newObj )
尝试使用:
let obj = {
'0': {ProId: "Space", Name: "cake", Quantity: "1"},
'1': {ProId: "new", Name: "walk", Quantity: "1"}
}
for(key in obj) {
let qty = String(Number(obj[key]['Quantity'])+Number(key))
obj[key]['Quantity'] = qrt;
}
一种方法是map
当前数组。
const info2 = info.map((product, index) => ({ ...product, Quantity: index + 1 }));
如果您想要Quantity
作为字符串,请执行以下操作Quantity: index + 1 + ''
或Quantity: (index + 1).toString()
。 什么都适合你。
你也可以试试这个:
let info = { 0: { ProId: "Space", Name: "cake", Quantity: "1" }, 1: { ProId: "new", Name: "walk", Quantity: "1" } } let index = 1; Object.values(info).map(function(val) { val.Quantity = index; index++; }); console.log(info);
我在你只使用对象的情况下做到了,没有设置为数组。
let info = {
0: { ProId: "Space", Name: "cake", Quantity: "1" },
1: { ProId: "new", Name: "walk", Quantity: "2" },
2: { ProId: "foo", Name: "bar", Quantity: "3" }
}
function incrementQuantify(obj) {
let _newInfo = {};
for (const key of Object.keys(obj)) {
info[key].Quantity = parseInt(info[key].Quantity, 10) + 1;
_newInfo[key] = info[key];
}
return _newInfo;
}
/*
{
'0': { ProId: 'Space', Name: 'cake', Quantity: 2 },
'1': { ProId: 'new', Name: 'walk', Quantity: 3 },
'2': { ProId: 'foo', Name: 'bar', Quantity: 4 }
}
*/
console.log(incrementQuantify(info));
更短的方法:
let info = { 0: { ProId: "Space", Name: "cake", Quantity: "1" }, 1: { ProId: "new", Name: "walk", Quantity: "2" }, 2: { ProId: "foo", Name: "bar", Quantity: "3" } }
for (const key of Object.keys(info)) info[key].Quantity = parseInt(info[key].Quantity, 10) + 1;
console.log(info);
edit: I already made an way to work with arrays too!
let info = [
{ ProId: "Space", Name: "cake", Quantity: "1" },
{ ProId: "new", Name: "walk", Quantity: "2" },
{ ProId: "foo", Name: "bar", Quantity: "3" }
]
function incrementQuantify(obj) {
let newObj = obj;
newObj.Quantity = parseInt(obj.Quantity, 10) + 1;
return newObj;
}
info.map(incrementQuantify);
/*
[
{ ProId: 'Space', Name: 'cake', Quantity: 2 },
{ ProId: 'new', Name: 'walk', Quantity: 3 },
{ ProId: 'foo', Name: 'bar', Quantity: 4 }
]
*/
console.log(info);
更短的方法:
let info = [ { ProId: "Space", Name: "cake", Quantity: "1" }, { ProId: "new", Name: "walk", Quantity: "2" }, { ProId: "foo", Name: "bar", Quantity: "3" } ]; info.map(o => o.Quantity = parseInt(o.Quantity, 10) + 1);
console.log(info);
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.