简体   繁体   中英

How to iterate array of object in JavaScript

My data looks like below:

 {
    "_id": "processedSKU_ID-1",
    "clientName": "fairmont",
    "searchQueryAnalysisObj": [{
        "searchKeyword": "iphone 1",
        "searchKeywordScore": 1.8000000000000003
    }, {"searchKeyword": "iphone 2", "searchKeywordScore": 5.400000000000001}, {
        "searchKeyword": "iphone 3",
        "searchKeywordScore": 1.8000000000000003
    }, {"searchKeyword": "iphone 4", "searchKeywordScore": 0.9000000000000001}],
    "_class": "com.unilog.model.MainClickStreamData"
}

I want to get all searchKeyword and searchScore .

I tried using:

for (var key in a)
    {
    
    a[key]['searchKeyword'].toString());
    
    }

but did not get anything.

You have to do loop on a.searchQueryAnalysisObj like:

 var a = { "_id" : "processedSKU_ID-1", "clientName" : "fairmont", "searchQueryAnalysisObj" : [ { "searchKeyword" : "iphone 1", "searchKeywordScore" : 1.8000000000000003 }, { "searchKeyword" : "iphone 2", "searchKeywordScore" : 5.400000000000001 }, { "searchKeyword" : "iphone 3", "searchKeywordScore" : 1.8000000000000003 }, { "searchKeyword" : "iphone 4", "searchKeywordScore" : 0.9000000000000001 } ], "_class" : "com.unilog.model.MainClickStreamData" }; for ( var key in a.searchQueryAnalysisObj ) { //a.searchQueryAnalysisObj[key]["searchKeyword"] <-- Will return searchKeyword //a.searchQueryAnalysisObj[key]["searchKeywordScore"] <-- Will return searchKeywordScore console.log( a.searchQueryAnalysisObj[key]["searchKeyword"] + " - " + a.searchQueryAnalysisObj[key]["searchKeywordScore"] ); }

Try this:

var a = {
    "_id": "processedSKU_ID-1",
    "clientName": "fairmont",
    "searchQueryAnalysisObj": [{
        "searchKeyword": "iphone 1",
        "searchKeywordScore": 1.8000000000000003
    }, {"searchKeyword": "iphone 2", "searchKeywordScore": 5.400000000000001}, {
        "searchKeyword": "iphone 3",
        "searchKeywordScore": 1.8000000000000003
    }, {"searchKeyword": "iphone 4", "searchKeywordScore": 0.9000000000000001}],
    "_class": "com.unilog.model.MainClickStreamData"
};

var analysis = a['searchQueryAnalysisObj'];
for( var i = 0, n = analysis.length; i < n; ++i ) {
    console.log(analysis[i]['searchKeyword'], '-', analysis[i]['searchKeywordScore']);
}

Output:

iphone 1 - 1.8000000000000003
iphone 2 - 5.400000000000001
iphone 3 - 1.8000000000000003
iphone 4 - 0.9000000000000001

You could take the wanted peroperty searchQueryAnalysisObj with array for iteration.

 var object = { _id: "processedSKU_ID-1", clientName: "fairmont", searchQueryAnalysisObj: [{ searchKeyword: "iphone 1", searchKeywordScore: 1.8000000000000002 }, { searchKeyword: "iphone 2", searchKeywordScore: 5.400000000000001 }, { searchKeyword: "iphone 3", searchKeywordScore: 1.8000000000000002 }, { searchKeyword: "iphone 4", searchKeywordScore: 0.9000000000000001 }], _class: "com.unilog.model.MainClickStreamData" }; object.searchQueryAnalysisObj.forEach(o => console.log(o));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

You can get the property searchQueryAnalysisObj which is an array infact. And then use loop or map etc

 var obj = { "_id" : "processedSKU_ID-1", "clientName" : "fairmont", "searchQueryAnalysisObj" : [ { "searchKeyword" : "iphone 1", "searchKeywordScore" : 1.8000000000000003 }, { "searchKeyword" : "iphone 2", "searchKeywordScore" : 5.400000000000001 }, { "searchKeyword" : "iphone 3", "searchKeywordScore" : 1.8000000000000003 }, { "searchKeyword" : "iphone 4", "searchKeywordScore" : 0.9000000000000001 } ], "_class" : "com.unilog.model.MainClickStreamData" } obj.searchQueryAnalysisObj.map((e)=>{ console.log(e.searchKeyword + " :: " + e.searchKeywordScore) })

Use forEach method like below:

var a  ={ "_id" : "processedSKU_ID-1", "clientName" : "fairmont", 
"searchQueryAnalysisObj" : [ { "searchKeyword" : "iphone 1", 
"searchKeywordScore" : 1.8000000000000003 }, { "searchKeyword" : "iphone 2", 
"searchKeywordScore" : 5.400000000000001 }, { "searchKeyword" : "iphone 3", 
"searchKeywordScore" : 1.8000000000000003 }, { "searchKeyword" : "iphone 4", 
"searchKeywordScore" : 0.9000000000000001 } ], "_class" : 
"com.unilog.model.MainClickStreamData" }
var b = a.searchQueryAnalysisObj

b.forEach( function (key, i)
 {
    console.log(key) //where key is each element of your array 'b'
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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