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!"
}
]
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
edited Mar 10, 2016 at 22:00
Dimitrios Vythoulkas
asked Mar 10, 2016 at 13:15
Dimitrios VythoulkasDimitrios Vythoulkas
6317 silver badges16 bronze badges
2
- what is your question? Do you have any problem doing it? – Nadir Commented Mar 10, 2016 at 13:17
- @Nadir I state at the bottom what my result needs to be. Obviously I cannot achieve it. – Dimitrios Vythoulkas Commented Mar 10, 2016 at 13:26
5 Answers
Reset to default 4Try 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.
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));
try
var newArr = messages.filter( function(value){
return value.id == 5;
} );
Use Array filter() method
messages.filter(function(msg){return msg['id'] === 5})
An approach pletely 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./jquery-1.11.3.min.js"></script>
<textarea id="result" style="width: 250px;height: 100px"></textarea>