0

I have some object with name and variables.

var data = [ {   "id": 144,
    "name": "Zagreb",
    "region": "",
    "iso": "HR",
    "country": "Croatia"},
{   "id": 145,
    "name": "Wellington",
    "region": "",
    "iso": "NZ",
    "country": "New Zealand"}];

For render dropdown I need only "name" and "country".

But, object can have other names with variables. Which I know. So I have idea, write names that needs to render in array like.

var renderNames = ['name', 'country'];

At this moment i've stopped. I don't know how to get value from object using names from array. As I see, it's must be something like that:

data.name == data. (do something) renderNames[1];
1
  • 1
    how do you want your output to look like? Commented Mar 29, 2016 at 11:36

4 Answers 4

2

For render dropdown I need only "name" and "country".

If you want an array of names from this array

var names = data.map(function(value){return value.name});

similarly, for array of country

var countries = data.map(function(value){return value.country});
Sign up to request clarification or add additional context in comments.

2 Comments

My guess is OP wants keys to be accessed from array
@RayonDabre maybe, but since he mentioned only that I need only "name" and "country" I am assuming that he somehow wants to use these two values for his dropdown rendering.
1

If you want an array with objects including name and country, this can work:

var dropdowns = data.map(function(obj) {
    return {
    country: obj.country,
    name: obj.name
  };
});

https://jsfiddle.net/w0ggxc3m/

Comments

0

How about using Lodash? https://lodash.com

Then it's easy

_.map(data, function(obj) {
  return _.pick(obj, ['name', 'country']);
});

Comments

0

You can also use arrow function expression new in ES6

var data = [ {   "id": 144,
    "name": "Zagreb",
    "region": "",
    "iso": "HR",
    "country": "Croatia"},
{   "id": 145,
    "name": "Wellington",
    "region": "",
    "iso": "NZ",
    "country": "New Zealand"}];
data.map(element => element.name) // return an array of names
//[ "Zagreb", "Wellington" ]
data.map(element => element.country) // return an array of country
//[ "Croatia", "New Zealand" ]    
data.map(element => [element.name, element.country]) // return 2d array of name, country
// [ [ "Zagreb", "Croatia" ], [ "Wellington", "New Zealand" ] ]

or return an array of objects where properties are "name" and "country"

data.map( element => { return { "name": element.name, "country": element.country }; }) 

which yields:

[
        {
                "name" : "Zagreb",
                "country" : "Croatia"
        },
        {
                "name" : "Wellington",
                "country" : "New Zealand"
        }
]

1 Comment

And with destructuring, data.map(element => element.name) can be written as data.map(({ name }) => name)

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.