padhiyar padhiyar - 4 months ago 10
Javascript Question

Find single value from multiple values in single array

I have a single array having multiple values in that as shown in code below.

tempstraindatasource

tempstraindatasource[0] = {
A = "0",
B = "1",
C = "2",
D = "3"
}

tempstraindatasource[1] = {
A = "4",
B = "5",
C = "6",
D = "7"


and so on like wise I have many data exist in single array
}

I want to do one procedure that finds the data are consist or not in single line like I want to find Species having name "A" = "0" or any value "B" = "0" or "C" = "0"! How can I do it in single line? Please some one help me to do this.

Thanks in advance and appreciate as well.

Answer

You could use Array#forEach for the array and check if any of the properties have the value, you need with Array#some, and push the index then to the result array.

var data = [{ a: 0, b: 1, c: 2, d: 3 }, { a: 4, b: 5, c: 6, d: 7 }],
    indices = [];

data.forEach(function (a, i) {
    Object.keys(a).some(function (k) {
        return a[k] === 0;
    }) && indices.push(i);
});

console.log(indices);

Comments