jdcaliff
jdcaliff

Reputation: 143

How to Get Value From an Array within a JS Object

I am trying to get a value from an array within an object.

This is the data:

{
    "Order Number":102541029, 
    "Tracking Number":192048236154915,
    "Secondary Tracking":87350125235,
    "Items":[{
     "SKU":"0200-02-01-NP-P-00",
     "QTY":4
    },
    {
     "SKU":"0120-02-01-XP-T-00",
     "QTY":2
    }]
   }

If I wanted, say, the quantity of the second item (SKU 0120-02-01-XP-T-00), how would I select this?

I've tried a few things like this:

var skuQty = datain.items.['0120-02-01-XP-T-00'].['QTY'];

That's the idea, but I am not using the right syntax obviously. Any help here?

Upvotes: 2

Views: 136

Answers (7)

Javier Rey
Javier Rey

Reputation: 1620

Make a function to find order items for a given SKU:

var findOrderItem = function (order, sku) {
  if (order && order.Items) {
    for (var i = 0; i < order.Items.length; i++) {
      if (order.Items[i].SKU == sku) {
        return order.Items[i];
      }
    }
  }
  return null;
};
var myOrder = {
  "Order Number": 102541029,
  "Tracking Number": 192048236154915,
  "Secondary Tracking": 87350125235,
  "Items": [
    {
      "SKU": "0200-02-01-NP-P-00",
      "QTY": 4
    },
    {
      "SKU": "0120-02-01-XP-T-00",
      "QTY": 2
    }
  ]
};
var sku = "0120-02-01-XP-T-00";
var item = findOrderItem(myOrder, sku);
var qty = 0;
if (item) {
  qty = item.QTY;
}
console.log("item qty", qty);

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

This is how you scroll through the quantities:

var yourObject = {
    "Order Number":102541029, 
    "Tracking Number":192048236154915,
    "Secondary Tracking":87350125235,
    "Items":[{
     "SKU":"0200-02-01-NP-P-00",
     "QTY":4
    },
    {
     "SKU":"0120-02-01-XP-T-00",
     "QTY":2
    }]
   };

var items = yourObject.Items;

for (var i = 0; i < items.length; i ++){
    console.log(items[i].QTY);
}

In general you can access an array with its index something like this: arr[i] and object can be accessed by it's key name:

 `yourObject.yourkey`

Upvotes: 1

tomato
tomato

Reputation: 93

Because items is an array, you need to select the appropriate index of the array, in this case 1. For example:

var skuQty = datain.items[1].QTY;

Upvotes: 0

guest271314
guest271314

Reputation: 1

You can use Array.prototype.filter(), at callback return o.SKU === "0120-02-01-XP-T-00", where o is current object in iteration use bracket notation to select element at index 0 of returned array

var QTY = data.Items.filter(function(o) {
            return o.SKU === "0120-02-01-XP-T-00"
          })[0]["QTY"];

You can alternatively utilize destructuring assignment

var [, {QTY}] = data.Items;

Upvotes: 0

kind user
kind user

Reputation: 41893

First of all, select the specified object - in your case - data.

Then select specified key from the data object - in your case - Items.

Since Items is an array with two objects, you have to specify which one of them you are interested in. In your case - the second one with index 1.

data.Items[1] is an object holding two positions. You are interested in the second one - so you just type that key name - QTY.

Adding it up together - data.Items[1].QTY.

var data = {
  "Order Number": 102541029,
  "Tracking Number": 192048236154915,
  "Secondary Tracking": 87350125235,
  "Items": [{
    "SKU": "0200-02-01-NP-P-00",
    "QTY": 4
  }, {
    "SKU": "0120-02-01-XP-T-00",
    "QTY": 2
  }]
};

console.log(data.Items[1].QTY)

Upvotes: 1

towc
towc

Reputation: 2034

in JavaScript, an array = [1,2,3] can be accessed with array[index].

If you have an array that looks like this: array = [{ prop: propValue }], then this is virtually the same as obj = { prop: propValue }; array = [ obj ], so what you need to do to get propValue is array[ 0 ]['prop'].

For your specific case, you'd need

datain.Items[1]['SKU'];

Now, what you actually want to do is filter through the array items until the above value is "0120-02-01-XP-T-00"

This is literally filtering an array:

datain.Items.filter( function( obj ){ return obj['SKU'] === "0120-02-01-XP-T-00" } )[0]['QTY']

The key is that array.filter( fn ) returns a new array of values for which fn( item [, other parameters you don't need to worry about] ) is true (or truthy). At that point, you only need the first item, at index = 0

Upvotes: 1

Mehdi Karamosly
Mehdi Karamosly

Reputation: 5438

This is how you get it:

Object {Order Number: 102541029, Tracking Number: 192048236154915, Secondary Tracking: 87350125235, Items: Array[2]}
obj.Items[0]
Object {SKU: "0200-02-01-NP-P-00", QTY: 4}
obj.Items[0].QTY

obj =  {
    "Order Number":102541029, 
    "Tracking Number":192048236154915,
    "Secondary Tracking":87350125235,
    "Items":[{
     "SKU":"0200-02-01-NP-P-00",
     "QTY":4
    },
    {
     "SKU":"0120-02-01-XP-T-00",
     "QTY":2
    }]
   };
console.log(obj.Items[0].QTY);

Upvotes: 0

Related Questions