1

I have an object as follows

{
  "s": "success",
  "result": {
    "XX-YY-12-33": {
      "quantity": "88",
      "warehouse_name": "USA Hub"
    },
    "AD-12-Y6-00": {
      "quantity": "99",
      "warehouse_name": "USA Hub"
    }
  }
}

The result object contains multiple objects which actually is the id of an sku

I want to access this each sku but don't have them separately anywhere. Can anyone tell how can I access them?

Starfish
  • 3,344
  • 1
  • 19
  • 47
  • what's the output you need? – XMehdi01 Aug 05 '22 at 19:47
  • did you try anything yet? – XMehdi01 Aug 05 '22 at 19:47
  • Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Aug 05 '22 at 19:48
  • I actually want to access the id which is like XX-YY-12-33 above and then i want to access count against each id... I have a large dataset – Shaheer Ilyas Aug 05 '22 at 19:49
  • I am able yo access till result by dot notation bit after that im stuck because the ids are unique and I don't have them anywhere – Shaheer Ilyas Aug 05 '22 at 19:51
  • You mean like `someObject.result["XX-YY-12-33"]?.quantity` ? Accessing the properties by string instead of dot notation? – Starfish Aug 05 '22 at 19:56
  • No i didn't mean that... I meant that i need to access the ids anyway as i then have to access the quantity related to each id...but it's nicely implemented by someone below – Shaheer Ilyas Aug 05 '22 at 20:13
  • I changed my answer to a more elegant version. Since you did noy tell what you wanted, my first version was simple. – mplungjan Aug 08 '22 at 19:08

2 Answers2

1

You mean this?

Object.entries and reduce

Had you been clearer, I would have given a more complex answer first time around

const obj = {
  "s": "success",
  "result": {
    "XX-YY-12-33": {
      "quantity": "88",
      "warehouse_name": "USA Hub"
    },
    "AD-12-Y6-00": {
      "quantity": "99",
      "warehouse_name": "USA Hub"
    }
  }
}

const res = Object.entries(obj.result)
  .reduce((acc, [key,{quantity}]) => { // spread the key and extracted quantity from value
    acc.keys.push(key);           // save the key
    acc.quantity.push(+quantity); // save the quantity (unary plus to convert to number) 
    acc.sum += +quantity;         // sum it
    return acc;
},{keys:[],quantity:[],sum:0});   // into an initialised object
console.log(res)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

Does this help?

I've put some comments in the code for further explanation.

const data = {
  "XX-YY-12-33": {
    "quantity": "88",
    "warehouse_name": "USA Hub"
  },
  "AD-12-Y6-00": {
    "quantity": "99",
    "warehouse_name": "USA Hub"
  }
};

// You can extract only the ids using Object.keys
const dataIds = Object.keys(data);

// Having the ref of the ids you can now iterate over them to extract the info you need per id
// Using array map function for example we can extract all the quantities:
const dataQtys = dataIds.map((id) => Number(data[id].quantity));
// alternatively you can use "Object.entries".

// If you want to sum all quantities you can use array reduce function:
const sum = dataQtys.reduce((res, qty) => res + qty, 0);

console.log('Ids: ', dataIds);
console.log('Quantities: ', dataQtys);
console.log('Sum: ' + sum);
Mauro Aguilar
  • 1,093
  • 13
  • 22