Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to iterate over the elements of an array and if a condition is true I want to create a new array.

Example: I have an array called Messages whose elements are objects and I want to check if the id attribute equals 5. If yes create a new array only consisting of this object.

messages = [{
  "id": 10,
  "body": "hello!"
}, {
  "id": 21,
  "body": "hola!"
}, {
  "id": 5,
  "body": "ciao!"
}];

var message5 = [];
var dataObj = {};

$.each(messages, function(index, value) {
  if (value.id == 5) {
    dataObj[index] = value;
  }
});

message5.push(dataObj[index]);

I want my result to be:

message5 = [
 {
  "id": 5,
  "body": "ciao!"
 }
]    
share|improve this question
    
what is your question? Do you have any problem doing it? – Nadir 21 hours ago
    
@Nadir I state at the bottom what my result needs to be. Obviously I cannot achieve it. – mosmic 21 hours ago
up vote 4 down vote accepted

Try to use Array.prototype.filter at this context,

var message5 = messages.filter(function(itm){
  return itm.id == 5;
});

console.log(message5);  //[{"id": 5,"body": "ciao!"}] 

If you want it to be more concise then you could use ES6 Arrow function,

var message5 = messages.filter(itm => itm.id == 5);
console.log(message5);  //[{"id": 5,"body": "ciao!"}]

But note that, the above code is similar to,

var message5 = messages.filter(function(itm){
  return itm.id == 5;
}.bind(this)); //Arrow function will automatically bind its lexical scope to it.
share|improve this answer
    
What if I want my message5 to be an object instead of an array? – mosmic 20 hours ago
    
@mosmic Then approach for that would be different. do you want me to write it now? :) – Rajaprabhu Aravindasamy 20 hours ago
    
If you don't mind, it would be helpful. – mosmic 15 hours ago
    
@mosmic Can you show me what is the expected result? Just give a sample – Rajaprabhu Aravindasamy 15 hours ago
    
message5 = {{"id":5,"body":"ciao!"}} – mosmic 12 hours ago

try to use filter:

var messages = [{
  "id": 10,
  "body": "hello!"
}, {
  "id": 21,
  "body": "holla!"
}, {
  "id": 5,
  "body": "ciao!"
}];
var message5 = messages.filter(function(message) {
  return message.id == 5;
});
alert(JSON.stringify(message5));

share|improve this answer

try

var newArr = messages.filter( function(value){

   return value.id == 5;

} );
share|improve this answer

Use Array filter() method

messages.filter(function(msg){return msg['id'] === 5})
share|improve this answer

An approach completely in jQuery could be:

$(function () {
  var messages = [{
    "id": 10,
    "body": "hello!"
  }, {
    "id": 21,
    "body": "holla!"
  }, {
    "id": 5,
    "body": "ciao!"
  }];
  var dataObj = [];

  $.each(messages, function (index, value) {
    if (value.id == 5) {
      dataObj.push(messages[index]);
    }
  });
  $('#result').text(JSON.stringify(dataObj, 4, 4));
});
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>


<textarea id="result" style="width: 250px;height: 100px"></textarea>

share|improve this answer
1  
This is what I had in mind. Obviously filter function simplifies everything, but your answer is very helpful because I see my mistake now. – mosmic 20 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.