0

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.

2
  • 1
    In what format? Please share expected output format. Commented Feb 9, 2018 at 9:25
  • You can't access searchkeyword directly, as it is a property of objects in an array searchQueryAnalysisObj Commented Feb 9, 2018 at 9:28

5 Answers 5

1

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"] );
}

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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; }

Comments

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)
})

Comments

0

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'
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.