0

Using vanilla Javascript, I would like to return the city name of multiple location as a String in the nested JSON example below. Using push doesn't work because of the complexity of the JSON structure (I can't change) which result in a separate loop.

"locations":[
    {
      "location":{
          "city":"New place 1",
          "other":"random"
      }
    },
    {
      "location":{
          "city":"New place 2",
          "other":"random dom"
      }
    }
]

Expected output: "New place 1", "New place 2"

Thanks in advance.

Sv443
  • 708
  • 1
  • 7
  • 27
Shiro
  • 39
  • 8
  • Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – Heretic Monkey Feb 22 '19 at 12:50

6 Answers6

0

Or, more easy solution.

let locations = [
    {
      "location":{
          "city":"New place 1",
          "other":"random"
      }
    },
    {
      "location":{
          "city":"New place 2",
          "other":"random dom"
      }
    }
];

let Array = [];
locations.forEach(function(par){
  Array.push(par.location.city);
})

let str = "";
Array.forEach(function(par){
  str += par +", ";
});

str = str.slice(0,-2);
  

console.log(str);
doğukan
  • 23,073
  • 13
  • 57
  • 69
0

You can use .reduce and template literals to build your string. Here I have also used .slice to remove the trailing comma and space at the end of your string:

const locs = {locations:[{location:{city:"New place 1",other:"random"}},{location:{city:"New place 2",other:"random dom"}}]},

res = locs.locations.reduce((acc, loc) => `${acc}${loc.location.city}, `, ``).slice(0,-2);
console.log(res);

If you just want an array as your output you can use .map:

 const locs = {locations:[{location:{city:"New place 1",other:"random"}},{location:{city:"New place 2",other:"random dom"}}]},
 
arr = locs.locations.map(loc => loc.location.city);
console.log(arr);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

You can use .map():

let locations = [
    {
      "location":{
          "city":"New place 1",
          "other":"random"
      }
    },
    {
      "location":{
          "city":"New place 2",
          "other":"random dom"
      }
    }
];

const result = locations.map(loc => loc.location.city);

console.log(result);
console.log(result.join(','));
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
0

Use Array.map and Array.join

let locations = [{"location":{"city":"New place 1","other":"random"}},{"location":{"city":"New place 2","other":"random dom"}}];

let result = locations.map(v => '"' + v.location.city + '"').join(", ");
console.log(result);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
0

const locs = [
    {
      "location":{
          "city":"New place 1",
          "other":"random"
      }
    },
    {
      "location":{
          "city":"New place 2",
          "other":"random dom"
      }
    }
]

const res = locs.map(d => d.location.city).join(', ');
console.log(res); // "New place 1, New place 2"

Nice and simple with locs.map(d => d.location.city).join(', ')

Dennis O'Keeffe
  • 506
  • 4
  • 7
0

One liner Using map and String constructor

var a = {
  locations: [{
    location: {
      city: "New place 1",
      other: "random"
    }
  }, {
    location: {
      city: "New place 2",
      other: "random dom"
    }
  }]
}
console.log(String(a.locations.map(e => e.location.city)))
ellipsis
  • 12,049
  • 2
  • 17
  • 33