1

Can't find a the correct workflow to get the following from looping thro array:

  • loop thro all the objects within the array and find a specific warehouse value
  • if the value exists in any object, use that object which holds the specific warehouse value and return a price value within it

For example: If I would do a search for 1374610389 warehouse value I would get returned price of 78.00

var test = [
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "10.00",
    warehouse: 1157964289
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "22.00",
    warehouse: 1269753487
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "78.00",
    warehouse: 1374610389
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "32.00",
    warehouse: 1674985630
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "16.00",
    warehouse: 1847893458
  }
]

2 Answers 2

2

The find() method returns a value of the first element in the array that satisfies the provided testing function.

You could use the find method :

test.find(obj => obj.warehouse == '1374610389').price

var test = [
    {
    lorem: "ipsum",
    dolor: "sit",
    price: "10.00",
    warehouse: 1157964289
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "22.00",
    warehouse: 1269753487
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "78.00",
    warehouse: 1374610389
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "32.00",
    warehouse: 1674985630
  },
  {
    lorem: "ipsum",
    dolor: "sit",
    price: "16.00",
    warehouse: 1847893458
  }
]

console.log( test.find(obj => obj.warehouse == '1374610389').price);

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

1 Comment

OP said if the value exists in any object, use that object,
1
var test = [{
  lorem: "ipsum",
  dolor: "sit",
  price: "10.00",
  warehouse: 1157964289
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "22.00",
  warehouse: 1269753487
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "78.00",
  warehouse: 1374610389
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "32.00",
  warehouse: 1674985630
}, {
  lorem: "ipsum",
  dolor: "sit",
  price: "16.00",
  warehouse: 1847893458
}];
for (var i = 0; i < test.length; i++) {
  if (test[i].warehouse == 1374610389) {
    console.log(test[i].price);
  }
}

Get the length of the array using the length propertie and enumerate over it using a for.

Also check Zakaria Acharki answer, his is way more elegant.

2 Comments

one quick question - can i use regex matching with this statement? For example: if warehouse contains first two numbers?
@g5wx warehouse is a number and I'd be really surprised to find out that regex work on numbers, but you could do something like test[i].warehouse.toString().match(expr) where expr is your regular expression, but I haven't really used regEx in javascript so not really sure

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.