1

I have not been able to figure out how to properly accomplish this.

I have a JS array of objects that looks like this:

[{"num":"09599","name":"KCC","id":null},{"num":"000027","name":"Johns","id":null}]

I would like to convert this into a simple, single JS array, without any of the keys, it should look like this:

[
  "09599",
  "KCC",
  "000027",
  "Johns" ]

The IDs can be dropped entirely. Any help would be really appreciated.

5
  • You don't want the ids? Commented Aug 10, 2015 at 14:49
  • no, thanks for reminding me Commented Aug 10, 2015 at 14:49
  • @op it is polite to show what you have tried so far in order to accomplish it, and explain why your attempts did not work for you. Commented Aug 10, 2015 at 14:51
  • 1
    check out stackoverflow.com/questions/921789/… for a generic strategy that could be applied to this problem Commented Aug 10, 2015 at 14:56
  • @wolffer-east thanks Commented Aug 10, 2015 at 15:04

3 Answers 3

4

Simply iterate the original array, pick the interesting keys and accumulate them in another array, like this

var keys = ['num', 'name'],
    result = [];

for (var i = 0; i < data.length; i += 1) {
  // Get the current object to be processed
  var currentObject = data[i];
  for (var j = 0; j < keys.length; j += 1) {
    // Get the current key to be picked from the object
    var currentKey = keys[j];
    // Get the value corresponding to the key from the object and
    // push it to the array
    result.push(currentObject[currentKey]);
  }
}
console.log(result);
// [ '09599', 'KCC', '000027', 'Johns' ]

Here, data is the original array in the question. keys is an array of keys which you like to extract from the objects.


If you want to do this purely with functional programming technique, then you can use Array.prototype.reduce, Array.prototype.concat and Array.prototype.map, like this

var keys = ['num', 'name'];

console.log(data.reduce(function (result, currentObject) {
  return result.concat(keys.map(function (currentKey) {
    return currentObject[currentKey];
  }));
}, []));
// [ '09599', 'KCC', '000027', 'Johns' ]
Sign up to request clarification or add additional context in comments.

7 Comments

This is very specific. Wouldn't Object.keys make this solution far more generic.
@Mouser OP didn't want the id to be extracted.
I know, but a black list is better then a white list, with many keys?
@Mouser It is subjective. What if the original object had 100 keys and OP wanted to get only 2 keys?
@thefourtheye Thank you so much, it works perfectly. Additionally thank you for taking the time to explain everything!
|
0

You can use Object.keys() and .forEach() method to iterate through your array of object, and use .map() to build your filtered array.

  var array = [{"num":"09599","name":"KCC","id":null},{"num":"000027","name":"Johns","id":null}];

  var filtered = array.map(function(elm){
    var tmp = [];
    //Loop over keys of object elm
    Object.keys(elm).forEach(function(value){
      //If key not equal to id
      value !== 'id'
      //Push element to temporary array
      ? tmp.push(elm[value])
      //otherwise, do nothing
      : false
    });
    //return our array
    return tmp;

  });

  //Flat our filtered array
  filtered = [].concat.apply([], filtered);

  console.log(filtered);
  //["09599", "KCC", "000027", "Johns"]

2 Comments

If we have to do it functional way, I have included a version in my answer. That would be better, I believe.
Yep, functional is the way to go :)
0

How about using map :

var data =  [
              {"num":"09599","name":"KCC","id":null} 
              {"num":"000027","name":"Johns","id":null}
            ];

var result = data.map(function(obj) {
   return [
       obj.num,
       obj.name,
       obj.id
    ];
});

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.