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

4

我有一个存储在变量 info 中的对象,如下:

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

我将尝试为每个索引将数量值更改为其index+1

我已经尝试使用静态值:

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

期望输出:

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

但是我想知道如何获取上述所提到的数量值。


1
为什么你使用了对象而不是数组? - Nina Scholz
1
最好使用数组来存储这些内容,因为无法保证对象排序的一致性。 - net.uk.sweet
1
我猜测这里的info是一个数组,因为在控制台上打印数组的格式与作者在帖子中的格式相同。@Codenewbie,console.log(Array.isArray(info))的结果是什么? - vidu.sh
1
@VidushanChooriyakumaran 真的。 - KcH
1
所以,你的变量 info 确实是一个数组。你可以查看下面的解决方案,建议使用 Array.forEachArray.map 进行循环。 - vidu.sh
显示剩余6条评论
7个回答

5

一些提示:

  • 如果需要一个易于迭代的数据结构,可以使用数组而不是对象。这样还可以控制迭代顺序。
  • 对于数字值,请使用数字类型。如果需要以后使用格式化字符串,则可以将其应用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);


2
您可以映射数组并返回一个新数组,但是可以将旧对象与具有更新的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 )


你能试着解释一下正在发生的事情吗? - jonatjano
但是我有我提到的数据,而不是你提到的那种方式。 - KcH
你有数据吗?你能分享实际的数据格式,而不是控制台日志视图吗? - palaѕн

1

Use forEach with index and add Quantity at certain index + index. And use toString() method as you want to have values in String format.

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


1
一种方法是对您当前的数组进行 map 映射。
const info2 = info.map((product, index) => ({ ...product, Quantity: index + 1 }));

如果您想将Quantity作为字符串处理,可以这样做:Quantity: index + 1 + '',或者Quantity: (index + 1).toString()。选择适合您的方法即可。

1
你也可以尝试这个:

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


1
我创建了这个对象,以便您可以仅使用对象而无需将其设置为数组。

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

编辑:我已经找到一种处理数组的方法!

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

希望能有所帮助!

1
尝试使用:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接