-1

I am pretty new to JavaScript and am trying to figure out how to find an item in an array. If I have:

var myArray = new Array();
myArray.push({ColumnA:1, ColumnB: 10, ColumnC:100);
myArray.push({ColumnA:1, ColumnB: 20, ColumnC:200);
myArray.push({ColumnA:2, ColumnB: 10, ColumnC:300);
myArray.push({ColumnA:2, ColumnB: 20, ColumnC:400);

If I want to return the ColumnC value for ColumnA = 1 and Column 2 = 20 how do I do so?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Swetzel
  • 3
  • 1
  • 3
  • 1
    Can we assume `myArray` is supposed to be `NECtable430250`? – Mark Jan 09 '19 at 19:30
  • 1
    You aren't closing your curly brace for creating the objects – Jay Mason Jan 09 '19 at 19:31
  • Possible duplicate of [Get JavaScript object from array of objects by value of property](https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property) and [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150) and [Find a value in an array of objects in Javascript](https://stackoverflow.com/questions/12462318) – adiga Jan 09 '19 at 19:32
  • 1
    Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – lux Jan 09 '19 at 19:35

3 Answers3

1

Simple, you can use something along the lines of Array.find, it seems like the most logical option, you could also use Array.filter to get multiple results.

var myArray = new Array();
myArray.push({ColumnA:1, ColumnB: 10, ColumnC:100});
myArray.push({ColumnA:1, ColumnB: 20, ColumnC:200});
myArray.push({ColumnA:2, ColumnB: 10, ColumnC:300});
myArray.push({ColumnA:2, ColumnB: 20, ColumnC:400});

var found = myArray.find(function(object) {
  return object.ColumnA == 1 && object.ColumnB == 20;
}); 

var foundMultiple = myArray.filter(function(object) {
  return object.ColumnA == 1 && object.ColumnB == 20;
});

console.log(found);
console.log(foundMultiple);
JO3-W3B-D3V
  • 2,124
  • 11
  • 30
0

First I would filter the array based on your criteria

var filteredArray = NECtable430250.filter(function (element) {
    return element.ColumnA == 1 && element.ColumnB == 20
});

//Produces [{ColumnA:1, ColumnB: 20, ColumnC:200}];

Next, you can get the single element in the filteredArray (I would recommend checking that it has at least one element) and get the value for ColumnC.

if(filteredArray.length > 0) {
    var ColumnC = filteredArray[0].ColumnC;
    //do stuff with ColumnC
}
Matt H
  • 1,795
  • 1
  • 7
  • 8
0

Simply find() those two values in the array like so:

 

var myArray = new Array();
myArray.push({ColumnA:1, ColumnB: 10, ColumnC:100});
myArray.push({ColumnA:1, ColumnB: 20, ColumnC:200});
myArray.push({ColumnA:2, ColumnB: 10, ColumnC:300});
myArray.push({ColumnA:2, ColumnB: 20, ColumnC:400});

var yourValue = myArray.find(e => e.ColumnA == 2 && e.ColumnB == 20 ? true : false);
console.log(theValue);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79