Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a JSON formatted array

I am trying to search the below array which is in JSON format.

[{"SystemID":"62750003","ApparentPower":"822"}, 
{"SystemID":"62750003","ApparentPower":"822"}, 
{"SystemID":"62750003","ApparentPower":"807"}, 
{"SystemID":"62750003","ApparentPower":"796"}]

I want to first check the first element in this case "SystemID" and append all the values of "SystemID" to op1 array I have created. I am not sure how to do this, my code to search the array is below (JS file):

$(document).ready(function() {
    $.ajax({
        url: "http://localhost/chartjs/data.php",
        method: "GET",
        success: function(data) {
            op1 = [];

            if (data[i] == 'SystemID') {
                for(var i in data) {
                    op1.push(data[i]['SystemID'])
                }
            }
        }
    } 
}    

When I run this code now, op1 is empty.

I want op1 to have all the values of SystemID from the JSON array.

UPDATE: I want to check IF the element is "SystemID" and if so, appened the first element to "op1". The first element and second element in my JSON data could change, so I want to check that first and then append the first element to "op1". Also I want to check the second element, and append the second elements value to "op2" array.

like image 906
Danny Avatar asked Nov 27 '25 03:11

Danny


1 Answers

As i understand you want all the SystemID in the array op1:

const data = [{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"822"}, {"SystemID":"62750003","ApparentPower":"807"},{"SystemID":"62750003","ApparentPower":"796"}];
const op1 = data.map(item => item.SystemID);

console.log(op1);
like image 140
Yosvel Quintero Arguelles Avatar answered Nov 29 '25 18:11

Yosvel Quintero Arguelles