0

I have an array of objects:

var arr = [
  {number: "AL-32021611", b: "7500"}, 
  {number: "AL-32021612", b: "Continental"}, 
  {number: "AL-32021612", b: "R3"}, 
  {number: "AL-32021612", b: "7500"}
];

Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?

for example

//loop

result = ["Continental", "R3", "7500"] 

what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences

2
  • So you want to discard the entries where there's a single occurrence of a particular value for number? Shouldn't the result be something like {"AL-32021612": ["Continental", "R3", "7500"]}? What should the result be if the first element of arr` is {number: "AL-32021611" , b: "United"} instead? Commented Jan 9, 2017 at 19:47
  • what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences Commented Jan 9, 2017 at 19:56

2 Answers 2

2

Using ES6 features:

let result = Array.from(new Set(arr.map(el => el.b)));

or

let result = [...new Set(arr.map(el => el.b))];
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think this is what OP wants. From the comments, it sounds like OP wants to supply a particular number value and obtain a list of corresponding b values.
0

Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.

As you have it,

result = {"Continental", "R3" , "7500"}; 

is not a valid object. You could use a for loop and push the b values into a new array that would look like:

result = ["Continental", "R3" , "7500"];

Your for loop would look like:

var result = [];
for(var n of arr) {
    result.push(arr[n].b);
}
return result;

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.