0

I want to get the message by key-word in the json structure. For example, the json structure is:

[{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}

Now I want to get all the message and put them into an arrary. How to implement this in javascript? Thanks

steveluscher
  • 4,144
  • 24
  • 42
Xiufen Xu
  • 531
  • 1
  • 3
  • 19

5 Answers5

2

var json = [{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}];
var arr = [];
for(i=0;i<json.length;i++) {
 arr.push(json[i].message);
}
console.log(arr);
chirag
  • 1,761
  • 1
  • 17
  • 33
1

You can use Array.map() or loop over the array and push the message to the array.

var a = [{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}];

var b = a.map(function(obj){
   return obj.message;
});

console.log(b);
Adam Azad
  • 11,171
  • 5
  • 29
  • 70
  • "Not bad code". Why do you need an `i` variable what is unused to? Try arrow function instead/ – Tân Oct 13 '16 at 05:37
  • 2
    @HappyCoding, I prefer sticking with a code that works for most browsers. ES6 is very new--I know it's exciting--but you need to either use a compiler like Babel or lots of polyfills. :) OP seems to be new to JavaScript, and still making their first steps. Don't rush into ES6 yet. – Adam Azad Oct 13 '16 at 05:40
  • @Adam Azad Thanks you so much, Adam! But I am just confusing what does the obj mean here? – Xiufen Xu Oct 13 '16 at 05:45
  • @XiufenXu, `obj` is the object like`{ _id: 123, message: 'hello', username: '1' }` for instance. It's a mere variable referring to the current object in the array. You can change it with any letter you want, but the shorter, the better. – Adam Azad Oct 13 '16 at 05:47
  • @Adam Azad Got you! Very clear explanation. Thank you so much – Xiufen Xu Oct 13 '16 at 05:51
  • @XiufenXu, you're welcomed :) – Adam Azad Oct 13 '16 at 05:51
1

This answers applies everywhere

var initialArray = [{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}]
var newArray = [];
for(i=0;i<initialArray.length;i++){
  newArray.push(initialArray[i].message)
}
console.log(newArray)

or shorter way to write above code

var initialArray = [{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}],
newArray = [];

for (i = 0; i < initialArray.length; i++) newArray.push(initialArray[i].message);
console.log(newArray);
4dgaurav
  • 11,360
  • 4
  • 32
  • 59
1

Use Array#map method with ES6 arrow function.

var data=[{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}];

var res = data.map(v => v.message);

console.log(res);

With traditional function instead of arrow function.

var data=[{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}];

var res = data.map(function(v){
   return  v.message;
});

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

An alternative is to use JsonPath: http://goessner.net/articles/JsonPath/

var messages = jsonPath(yourArray, "$..message");

In my opinion the easiest ways to find stuff in json.

Chrisposure
  • 167
  • 1
  • 14