How to select a particular field from javascript array
Asked Answered
H

5

13

I have an array object in javascript. I would to select a particular field from all the rows of the object.

I have an object like

var sample = {
[Name:"a",Age:1],
[Name:"b",Age:2],
[Name:"c",Age:3]
}

I would like to get an output of only Names as ["a","b","c"] without looping over sample object.

How can I select one or two fields using jlinq? or any other plugin?

Thanks a lot.

Hewett answered 24/8, 2011 at 8:3 Comment(2)
Your object is invalid. That should be an array.Deidradeidre
@Dogbert-sorry the outer braces should be [] and inner should {}Hewett
E
8

You've got your definition the wrong way round. Instead of having an object containing 3 arrays, you want an array of objects.

like this:

var sample = [{Name:"a",Age:1},
   {Name:"b",Age:2},
   {Name:"c",Age:3}];

Then you can do:

var name0 = sample[0].Name;
var age0 = sample[0].Age;

or to get all your names as per your example:

var names = [sample[0].Name,sample[1].Name,sample[2].Name];

But, without looping im not sure how you would get any number of values.... why no looping?

Just say you do loop, here's how you would do it:

var names = []
for(x in sample)
   names.push(sample[x].Name);

or with jQuery (which is still looping)

sample= jQuery.map(sample, function(n, i){
  return n.Name;
});
Elaina answered 24/8, 2011 at 8:8 Comment(0)
J
17

You can try this:

var sample = [{Name:"a", Age:1}, {Name:"b", Age:2}, {Name:"c", Age:3}];
var Names = sample.map(function(item){return item.Name;});
Judgemade answered 24/8, 2011 at 8:8 Comment(3)
array.map will not be universally supported by all browsers. Use the jQuery version and you're safe.Elaina
@Jamiec: (...) At the time of writing “Array extras” (which are actually standardized methods, rather than extras) are supported by the new versions of all major browsers. Unless stated otherwise, all the discussed methods can be safely used in: Opera 11+ Firefox 3.6+ Safari 5+ Chrome 8+ Internet Explorer 9+ (source: dev.opera.com)Rusell
Thanks, its working. But basically it also loops only so I think I cant avoid looping even indirecty. Since I am using IE 8 I will go with jquery map.Hewett
E
8

You've got your definition the wrong way round. Instead of having an object containing 3 arrays, you want an array of objects.

like this:

var sample = [{Name:"a",Age:1},
   {Name:"b",Age:2},
   {Name:"c",Age:3}];

Then you can do:

var name0 = sample[0].Name;
var age0 = sample[0].Age;

or to get all your names as per your example:

var names = [sample[0].Name,sample[1].Name,sample[2].Name];

But, without looping im not sure how you would get any number of values.... why no looping?

Just say you do loop, here's how you would do it:

var names = []
for(x in sample)
   names.push(sample[x].Name);

or with jQuery (which is still looping)

sample= jQuery.map(sample, function(n, i){
  return n.Name;
});
Elaina answered 24/8, 2011 at 8:8 Comment(0)
C
2

That Javascript has no meaning. It is syntatically incorrect. I assume you meant:

var sample = [
    {Name:"a",Age:1},
    {Name:"b",Age:2},
    {Name:"c",Age:3}
]

Then you can use jQuery to do something like this:

var names = $(sample).map(function(){ return this.Name; });

But in reality all jQuery is doing is looping through the object for you. Writing your own loop would be (insignificantly) faster. There is no way to do this without a loop.

Carley answered 24/8, 2011 at 8:12 Comment(0)
H
1
let names = [];
this.dtoArray.forEach(arr => names.push(arr.name));
Hamper answered 14/2, 2022 at 6:39 Comment(0)
P
0
// assuming you mean:
var sample = [
    {Name:"a",Age:1},
    {Name:"b",Age:2},
    {Name:"c",Age:3}
]

Well, your biggest problem there is that no matter what happens, you'll have to loop. Now, jQuery will let you hide that behavior with:

var output = []
$.each( sample, function(i,e){ output.push( e.Name )} );

But fundamentally, that is the same thing as:

for( var i = 0; i < sample.length; i++ )
{
   output.push( v.Name )
}
Pinworm answered 24/8, 2011 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.