batMan007
batMan007

Reputation: 589

How to parse [object Object]

I don't know how to parse the value from response. I need to get a value according to key and values in individual strings but couldn't make it work

here my sample code

[
{"columns":
["campName","page_visits","filter_types","searchUrl","pageNo"],
"values":[["kk","2","All","https://www.google.com/search/results/people/?keywords=kk&origin=SWITCH_SEARCH_VERTICAL",1]]}]

I want campName,page_visits,filter_types,searchUrl,pageNo values to be stored as a string in javascript

Upvotes: 0

Views: 909

Answers (2)

Hassan Imam
Hassan Imam

Reputation: 22564

You can create an object using array#reduce

var arr = [{"columns":["campName","page_visits","filter_types","searchUrl","pageNo"],"values":[["kk","2","All","https://www.google.com/search/results/people/?keywords=kk&origin=SWITCH_SEARCH_VERTICAL",1]]}];

var result = arr[0].columns.reduce((o, a, i) => {
  o[a] = arr[0].values[0][i];
  return o;
}, Object.create(null));

console.log(result);

Upvotes: 1

Erazihel
Erazihel

Reputation: 7605

You can store these values in an object:

const data = [{
  "columns": ["campName", "page_visits", "filter_types", "searchUrl", "pageNo"],
  "values": [
    ["kk", "2", "All", "https://www.google.com/search/results/people/?keywords=kk&origin=SWITCH_SEARCH_VERTICAL", 1]
  ]
}]

const result = data[0].columns.reduce((acc, curr, index) => {
  acc[curr] = data[0].values[0][index];
  
  return acc;
}, {});

console.log(result);

Upvotes: 2

Related Questions