4

I have an object in variable info as :

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 ,

I have tried to have a static value as:

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

Expected 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

11
  • 1
    why do you have an object instead of an array? Commented Feb 27, 2020 at 13:11
  • 1
    You'd be better using an array to store these as there's no guarantee of object ordering being consistent. Commented Feb 27, 2020 at 13:11
  • 1
    I'm probably guessing it here but I believe info is an Array as console.log on an array is formatted the way author has formatted on his post @Codenewbie what is the result of console.log(Array.isArray(info)) ? Commented Feb 27, 2020 at 13:29
  • 1
    @VidushanChooriyakumaran true Commented Feb 27, 2020 at 13:33
  • 1
    So, your variable info is indeed an Array. You can have a look at the solutions below suggesting to loop through it with Array.forEach or Array.map Commented Feb 27, 2020 at 13:40

7 Answers 7

5

Some hints:

  • Take an array instead of an object, if you need an easy iterable data structure. This gives control over the iteration order, too.
  • Use numbers for numeric values. If you need later a (formatted) string apply toString to it.

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can map the array and return a new array but merging the old object with new object with updated Quantity value like:

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:

let obj = { a: 0, b: 0};
let newObj = { ...obj, a: 1 };

console.log( newObj )

3 Comments

can you try giving some explanations of what's going on please
But I have the data as I mentioned , not the above way you mentioned
How you have the data like? Can you share the actual data format, not the console log view?
1

One way is to map your current array.

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(). Whatever suits you.

Comments

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

Comments

1

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

Comments

1

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

Comments

1

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.