0

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

Itchydon
  • 2,572
  • 6
  • 19
  • 33
batMan007
  • 551
  • 1
  • 10
  • 24

2 Answers2

2

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);
Erazihel
  • 7,295
  • 6
  • 30
  • 53
1

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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51