2

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?

  • Jesse
1

7 Answers 7

1

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`
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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)

Comments

0

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

Comments

0

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;

Comments

0

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;

1 Comment

Ty, edited the code block with the appropriate changes.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.