0

I currently have an array of objects where each object has several properties. Example:

[
   { text: 'test1',
     id: 1
   },
   { text: 'test2',
     id: 2
   }
]

What would be the best way to convert this to an array of strings that contains the value from text? I had thought I might be able to do this using underscore.js:

headerText = _.pick(headerRow, 'text');

But I think that since the objects are in an array this will not work. My next idea is to just loop through each element in the array and push the text value to a new array, but i'm curious if anyone knows of a more elegant way to do this? Suggestions?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

4 Answers4

4

You're looking for Array#map:

var stringArray = headerRow.map(function(entry) {
    return entry.text;
});

Live Example | Source

You don't even need Underscore, Array#map is part of ES5 and fully supported by V8, the JavaScript engine used by Node. Array#map calls the function you give it once for each entry in the array, and builds a new array from the return values of that function.

Or if you want to change the existing array, you can use Array#forEach:

headerRow.forEach(function(entry, index) {
    headerRow[index] = entry.text;
});

Live Example | Source

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Use _.map(headerRow, function(row) { return row.text; }). Array.map isn't available in IE < 9.

ebsddd
  • 1,028
  • 9
  • 20
0

i'd use a foreach and just loop through it.

 var jamie = [
    { text: 'test1',
      id: 1
    },
    { text: 'test2',
      id: 2
    }
 ];

 var length = jamie.length,
     element = [];
 for (var i = 0; i < length; i++) {
   element[i] = jamie[i].id;
   // Do something with element i.
 }
   console.info(element);
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
-1

This is a vanilla javascript version, which avoids using the not universally supported Array.map method.

// assign the array to a variable
var a = [
   { text: 'test1',
     id: 1
   },
   { text: 'test2',
     id: 2
   }
];

// loop through each item in the array, reassigning with it's text value
// not like this: for(i in a) a[i] = a[i].text
// but with a for loop based on the array length
var i;
for(i=a.length; i; i--){ a[i-1] = a[i-1].text; }

// check the results
console.log(a);
// ["test1", "test2"]
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • `for(i=0;i – Plato Mar 04 '13 at 16:54
  • Note: comment seems to have gone linking to [why not to use `for...in` to enumerate arrays](http://blog.niftysnippets.org/2010/11/myths-and-realities-of-forin.html) – Billy Moon Mar 04 '13 at 17:02
  • `for i in a` could find undesired prototypical stuff – Plato Mar 04 '13 at 17:10
  • @BillyMoon: I see a couple of possible reasons: 1. You never declare `i` and thus fall prey to *The Horror of Implicit Globals*, and 2. You're abusing the `for` loop (modifying the indexer in the test is an anti-pattern -- the downvoter probably thought the loop body saw the wrong `i` value, which is one reason it's best not to abuse the `for` loop). Then there's the fact you're relying on the horror that is Automatic Semicolon Insertion, but anyone who downvotes for *that* needs talking to. :-) – T.J. Crowder Mar 04 '13 at 17:21
  • @T.J.Crowder thanks for the thoughtful insight. I am only coding like this for demonstration here, in my own code, I will add semicolons etc... but I feel that it is more useful to put as little in there that does not directly relate to the question (global declarations / semi-colons/etc..). There was recently an issue with someone copy/pasting my code from stack overflow, causing the stack overflow site to come second in google when searching for `stack overflow`, so maybe I should start writing more pristine code from now on. – Billy Moon Mar 04 '13 at 18:41
  • @T.J.Crowder do you think that there is a problem with the `anti-pattern` way of using a for-loop? Is it for readability that it is not advised? – Billy Moon Mar 04 '13 at 18:46
  • @BillyMoon: Maintainability, which encompasses readability, yes. I have a rule about `for` loops: If you're not using each of the three parts of it, and for its primary purpose, use a different kind of loop. In this case, for instance, if you wanted to use the decrement trick I'd've recommended `i = a.length; while (i--) { ... }`. (Personally, I just prefer the `for` starting with `- 1`.) As for the other stuff, people absolutely **will** use your code verbatim, so making it less than you believe to be the correct standard is probably not serving them very well. – T.J. Crowder Mar 04 '13 at 22:03
  • @BillyMoon: ...and I'm pretty sure stackoverflow.com has been the first Google hit for `stack overflow` for a long time now. ;-) – T.J. Crowder Mar 04 '13 at 22:06
  • @T.J.Crowder Well read [this](http://meta.stackexchange.com/questions/169405/google-indexing-issue-for-keyword-stackoverflow) and [this](http://news.ycombinator.com/item?id=5311151) and see the [troublesome answer](http://stackoverflow.com/questions/5411538/how-to-redirect-from-html-page/5411601#5411601) of mine causing the issue – Billy Moon Mar 04 '13 at 22:25