简体   繁体   English

从json数据中选择特定的键

[英]selecting particular keys from json data

I have a array as: 我有一个数组为:

var cols = ["ticker", "highPrice", "lowPrice","lastPrice"] // dynamic

Json data comming from backend as: 来自后端的Json数据来自:

info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84}

suppose I want to select market cap then I can do info.marketCap . 假设我要选择市值,则可以执行info.marketCap But I want to select only those json values which keys are equals to cols ie info.ticker, info.highPrice, info.lowPrice 但我只想选择键等于cols的那些json值, ie info.ticker, info.highPrice, info.lowPrice

and assign N/A to those which is undefined in json but present in cols array ie info.lastPrice = "N/A" 并将N / A分配给json中未定义但存在于cols数组中的那些,即info.lastPrice = "N/A"

Note: cols changes from time to time 注意:cols不时更改

Here is what I have got so far 这是到目前为止我得到的

SyScreener.fillScreenerResult = function(info) {
        var cols = ["ticker", "highPrice", "lowPrice", "openPrice", "lastPrice", "currentVol", "avgVol"];
        var data = [];
        for(var i=0; i<info.length; i++) {
            var jsonKeys = Object.keys(info[i]);
            for(var j=0; j<jsonKeys.length; i++) {
                if(cols.contains(jsonKey[j])) {
                    // TODO something like - data.push([info[i].jsonKey[j])
                } else {
                    // TODO something like - info[i].colsValue = "N/A"
                }
            }
        }
        SyUtils.createDataTable("screener_result", data);   
    };

do you mean something like this: 你的意思是这样的:

var cols = ["ticker", "highPrice", "lowPrice","lastPrice"];
info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84};
for(var c = 0, clen = cols.length; c < clen; c++) {
  if( !(cols[c] in info) ) {
     console.log("N/A");
  }
  else {
    console.log(info[cols[c]]);
  }
}

Demo:: jsFiddle 演示:: jsFiddle

I may not be reading your question correctly but from my understanding I might suggest something like this. 我可能没有正确阅读您的问题,但据我了解,我可能会建议类似的事情。

for (var i=0; i<cols.length; i++) {
    var fieldName = cols[i];
    if (!info.hasOwnProperty(fieldName)) {
        info[fieldName] = 'N/A';
    }
}

This simply iterates through each field name in cols and checks if it is a property of the info JSON object. 这只是简单地遍历cols中的每个字段名称,并检查它是否是info JSON对象的属性。 If it isn't already present the loop adds the property with a value of 'N/A'. 如果尚不存在,则循环将添加值为“ N / A”的属性。

var cols = ["ticker", "highPrice", "lowPrice","lastPrice"]

var info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84}

var output = {};
for(each in info) {
    var index = cols.indexOf(each)
    if(index != -1) {
        output[each] = info[each];
        //remove extra already checked element
        cols.splice(index,1)
    }
}
//append remaining keys
for(var i=0;i<cols.length;i++) {
    output[cols[i]] = "N/A"
}

console.log(output)
//output Object {ticker: "AAPL", lowPrice: 42.72, highPrice: 42.84, lastPrice: "N/A"} 

First thing, you nedd deserialize you JSON data. 首先,您需要对JSON数据进行反序列化。 To do this using jQuery use the function parseJson that you can find here http://api.jquery.com/jquery.parsejson/ once you deserialized it, you can do whatever you want with this data since you manipulate it as a plain javascript array. 要使用jQuery来执行此操作,请使用parseJson函数,在将其反序列化后,您可以在此处http://api.jquery.com/jquery.parsejson/找到该数据,因为可以将其作为普通的javascript进行操作,因此可以对其进行任何处理。阵列。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM