简体   繁体   English

如何将对象中数组的索引值添加到键

[英]How to add index values of array in object to a key

I have an object in variable info as :我在变量info有一个对象:

0: {ProId: "Space", Name: "cake", Quantity: 1}
1: {ProId: "new", Name: "walk", Quantity: 1}

I am trying to change the Quantity value to it's index+1 for each index ,我正在尝试将 Quantity 值更改为每个索引的index+1

I have tried to have a static value as:我试图有一个静态值:

  for (var key in obj){
    var value= obj[key];
    value['Quantity'] = 5;
  }

Expected O/p:预期 O/P:

console.log(info)
    0: {ProId: "Space", Name: "cake", Quantity: 1}
    1: {ProId: "new", Name: "walk", Quantity: 2}
    2: {............................Quantity: 3}.....

But May I know how can I have the Quantity value as above mentioned但是我可以知道如何获得上述Quantity值吗?

Some hints:一些提示:

  • Take an array instad of an object, if you need an easy iterable data structure.如果您需要简单的可迭代数据结构,请使用对象的数组实例。 This gives a control over the iteration order, too.这也可以控制迭代顺序。
  • Use numbers for number values.使用数字作为数字值。 If you need later a (formatted) string apply toString to it.如果您稍后需要一个(格式化的)字符串,将toString应用到它。

For getting a new value of a property, you could mutate the given data为了获得一个属性的新值,你可以改变给定的数据

Using:使用:

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

Or get new objects by mapping the array.或者通过映射数组来获取新对象。

Using:使用:

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

Use forEach with index and add Quantity at certain index + index .forEachindex 一起使用并在特定 index + index处添加Quantity And use toString() method as you want to have values in String format.并使用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);

You can map the array and return a new array but merging the old object with new object with updated Quantity value like:您可以映射数组并返回一个新数组,但将旧对象与具有更新的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; }

A simple example of updating an object key value (which is done here inside the map() method) is like:更新对象键值的简单示例(在map()方法中完成)如下:

 let obj = { a: 0, b: 0}; let newObj = { ...obj, a: 1 }; console.log( newObj )

Try using:尝试使用:

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

One way is to map your current array.一种方法是map当前数组。

const info2 = info.map((product, index) => ({ ...product, Quantity: index + 1 }));

In case you want Quantity as a string do this Quantity: index + 1 + '' , or Quantity: (index + 1).toString() .如果您想要Quantity作为字符串,请执行以下操作Quantity: index + 1 + ''Quantity: (index + 1).toString() Whatever suits you.什么都适合你。

You can also try this:你也可以试试这个:

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

I made it in the case you are using just the object, without set up as an array.我在你只使用对象的情况下做到了,没有设置为数组。


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

Shorter way:更短的方法:

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

Shorter way:更短的方法:

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

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM