搜索JSON格式的数组

时间:2019-02-22 10:39:38

标签: javascript php json

我正在尝试搜索以下JSON格式的数组。

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

在这种情况下,我想先检查第一个元素“ SystemID”,并将“ SystemID”的所有值附加到我创建的op1数组中。我不确定如何执行此操作,我搜索数组的代码如下(JS文件):

$(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'])
                }
            }
        }
    } 
}    

现在运行此代码时,op1为空。

我希望op1具有JSON数组中所有SystemID的值。

更新:我要检查元素是否为“ SystemID”,如果是,则将第一个元素应用于“ op1”。 JSON数据中的第一个元素和第二个元素可能会更改,因此我想先检查一下,然后将第一个元素附加到“ op1”。另外,我想检查第二个元素,并将第二个元素的值附加到“ op2”数组。

6 个答案:

答案 0 :(得分:1)

据我了解,您希望数组SystemID中的所有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);

答案 1 :(得分:0)

您需要类似的内容,您缺少for循环

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

    var op1 = [];
    var op2 = [];
    for(let i=0; i < data.length;i++){
      for(let item in data[i]){
         if(item == "SystemID"){
           op1.push(data[i][item])
         }
         if(item == "ApparentPower"){
           op2.push(data[i][item])
         }
      }
    }


console.log(op1)
console.log(op2)

如果您想使用ECMA6

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

        let op1 = data.map(item => item.SystemID);
        let op2 = data.map(item => item.ApparentPower);
      
    console.log(op1)
    console.log(op2)

如果您想要全球概念

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


let data_keys = Object.keys(data[0])
let result = []
for(let item in data_keys){
  result.push([])
}
  for(let i=0; i < data.length;i++){
    for(key in data_keys){
      if(Object.keys(data[i])[key] == data_keys[key]){
        result[key].push(Object.values(data[i])[key])
      }
    }
  }



for(let k=0; k<result.length;k++){
console.log(result[k])
}
console.log(result)

答案 2 :(得分:0)

据我了解,您的代码将是这样

 var json = [{"SystemID":"62750003","ApparentPower":"822"}, 
     {"SystemID":"62750003","ApparentPower":"822"}, 
     {"SystemID":"62750003","ApparentPower":"807"}, 
    {"SystemID":"62750003","ApparentPower":"796"}];


     var myArray =[];

       json.forEach(function (item) { 
          if(item.SystemID) {
               	myArray.push(item.SystemID);
           }
       });

       console.log(myArray);

答案 3 :(得分:0)

您要检查第一个密钥是否特别是SystemID?如果是这种情况,您可以尝试

const data = [
  {"SystemID":"62750003","ApparentPower":"822"}, 
  {"SystemID":"62750003","ApparentPower":"822"}, 
  {"SystemID":"62750003","ApparentPower":"807"}, 
  {"SystemID":"62750003","ApparentPower":"796"}, 
  {"NOTASystemID":"6sss","ApparentPower":"796"}
];

const op = data
  .filter(element => Object.keys(element)[0]==="SystemID") //filtering out element with first key sysID
  .map(element => element.SystemID);

console.log(op);

答案 4 :(得分:0)

假设您已经解析了JSON数据,则可以通过数组进行映射并使用hasOwnProperty()方法来检查数组中的每个对象是否具有SystemID,如果存在,请按它是op1数组的值。

由于对象属性未排序,因此尝试使用索引检索对象中的元素并不可靠。

var data = [{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"807"},{"SystemID":"62750003","ApparentPower":"796"}];

var op1 = data.map(e => {
  if (e.hasOwnProperty("SystemID")) {
    return e.SystemID;
  } else {
    // do something else since current mapped object doesn't have "SystemID"
  }
});

console.log(op1);

答案 5 :(得分:0)

尝试一下

var data = '[{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"822"},{"SystemID":"62750003","ApparentPower":"807"},{"SystemID":"62750003","ApparentPower":"796"}]';

 var op1 = [];
 $.each(JSON.parse(data),function(i,item){
   op1[i] = item.SystemID;
 });
 console.log(op1);