admin管理员组文章数量:1316826
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>
本文标签: javascriptUse conditional inside each function to create arrayStack Overflow
版权声明:本文标题:javascript - Use conditional inside $.each function to create array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014971a2413597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论