0

I have an issue where an API call I'm using is sending objects with one property that contains a single array value (keys property in response below). Unfortunately I cannot use this format as I must abide by Nested arrays in order to use the outputted values in a separate application like so [[value1, value2, value3, value4],[value1, value2, value3, value4]]. I plan on asking a separate question to tackle the nested array section unless someone thinks it is an easy fix (I believe I should use .map to convert the object).

Here is the format of my objects (from console.log(searchQueries)):

[ { keys: [ 'hammer' ],
    clicks: 1369,
    impressions: 3151,
    ctr: 0.4344652491272612,
    position: 1.004443033957474 },
  { keys: [ 'woodmaking' ],
    clicks: 207,
    impressions: 6324,
    ctr: 0.03273244781783681,
    position: 4.35831752055661 },
  { keys: [ 'house trends' ],
    clicks: 1,
    impressions: 3,
    ctr: 0.3333333333333333,
    position: 4.666666666666666 },
  { keys: [ 'housing' ],
    clicks: 1,
    impressions: 36,
    ctr: 0.027777777777777776,
    position: 6.472222222222222 } ]
byProperty

Above response is passed from the following for-in loop the result of this API response array being nested in an object originally:

for (var prop in res){
              searchQueries = res[prop];

              console.log(searchQueries);
}

Would the JSON.stringify method or .toString('keys') achieve what I'm looking for?

2
  • 2
    Have you tried JSON.stringify or toString? Commented Jan 31, 2017 at 16:45
  • Yes, JSON.stringify just took my variable, parsed it for jus the keys values and outputted that result. toString('keys') came back with an error Commented Jan 31, 2017 at 17:12

3 Answers 3

2

If you want to turn keys from an array into a string, you just need to iterate over your array and make the change:

searchQueries.forEach(function (obj) { obj.keys = obj.keys[0] })
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer. I apologize, but I had forgotten what loop was being used to output the results. There is a for-in loop with results being passed to searchQueries variable. I believe this might be why I receive a TypeError: searchQueries.forEach is not a function at .forEach. since it is nested within an object. Would you agree?
@cphill what does console.log(typeof searchQueries); return?
@Jonasw object string
@cphill So you need to convert the main Object into an Array too right? ive edited my answer...
1
 answer=Object.values(searchQueries).map(el=>{el.keys=el.keys[0];return Object.values(el)});
console.log(searchQueries);

https://jsbin.com/biyazunafu/1/edit?console

Loop over the main array, turn the Objects (el) keys array into a string, than turn the whole object into its values Array. However, Object.values is experimental, so may dont use this on the users side, instead use this code transpiled to ES5 :

answer=[];
for(key in searchQueries){
  answer.push(searchQueries[key]);
 }
answer=answer.map(function(el){
  el.keys=el.keys[0];
  var newel=[];
  for(key in el){
    newel.push(el[key]);
  }
 return newel;
});

5 Comments

Can you explain this one liner?
@Jonasw thank you for the help, but do you mind converting your answer to something that doesn't use experimental methods and ES6?
@cphil shure. However i recommend you to use babel, as ES6 is quite cool...
@Jonasw thank you again for your help and I apologize for how long it took to accept the answer. I get the code to work in JS Bin, but I'm running into an error at el.keys=el.keys[0] within my code. It is coming back as TypeError: Cannot read property '0' of undefined. When I console log el.keys I get back [Function: keys] and when I console log el.keys[0] I get undefined. Do you know what [Function: keys] means?
@Jonasw disregard my comment above. I fixed it.
0
  • 1st Get the keys values

    var keys = searchQueries.map(function(e){return e.keys;});
    

This will output :

[["hammer"], ["woodmaking"], ["house trends"], ["housing"]]
  • 2nd: Concat the resulting array

    var values = keys.join(',').split(',');
    

The result :

 ["hammer", "woodmaking", "house trends", "housing"]

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.