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

2
  • 2
    Possible duplicate of Parse JSON in JavaScript? Commented Aug 17, 2017 at 12:15
  • i tried its not working Commented Aug 17, 2017 at 12:15

2 Answers 2

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

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

1 Comment

i need value for campName = kk , page_visits = 2, filter_types = All
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);

1 Comment

That is the same answer as Erazihel gave a few minutes earlier, why duplicate it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.