簡體   English   中英

如何將對象中數組的索引值添加到鍵

[英]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應用到它。

為了獲得一個屬性的新值,你可以改變給定的數據

使用:

 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);

forEachindex 一起使用並在特定 index + index處添加Quantity 並使用toString()方法,因為您希望獲得String格式的值。

 var obj = [{ProId: "Space", Name: "cake", Quantity: "1"}, {ProId: "new", Name: "walk", Quantity: "1"}] obj.forEach(function(el,idx){ el.Quantity=(idx+parseInt(el.Quantity)).toString() }) console.log(obj);

您可以映射數組並返回一個新數組,但將舊對象與具有更新的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM