admin管理员组文章数量:1410725
I'm not sure if this is possible, because I have not found anything on this.. I am going through a JSON object..
{"name": "zack",
"message": "hello",
"time": "US 15:00:00"},
{"name": "zack",
"message": "hello",
"time": "US 00:00:00"}
Is there a way I can select the time property that contains just the "15:00:00" part?
Thanks for the help
I'm not sure if this is possible, because I have not found anything on this.. I am going through a JSON object..
{"name": "zack",
"message": "hello",
"time": "US 15:00:00"},
{"name": "zack",
"message": "hello",
"time": "US 00:00:00"}
Is there a way I can select the time property that contains just the "15:00:00" part?
Thanks for the help
Share Improve this question edited Apr 10, 2018 at 8:06 Allison asked Oct 13, 2017 at 5:07 AllisonAllison 652 silver badges9 bronze badges 1- Could you be a little bit more clear about what you mean? What do you mean by select? – Derek Brown Commented Oct 13, 2017 at 5:14
4 Answers
Reset to default 1As I understand if you parse your JSON you have an array of object. So you can make use of filter function and filter out those elements that don't match the criteria you pass in filter function:
var parsedJson = [{"name": "zack",
"message": "hello",
"time": "US 15:00:00"},{"name": "zack",
"message": "hello",
"time": "US 00:00:00"}];
var result = parsedJson.filter(item=>item.time === "US 15:00:00");
console.log(result);
You can use filter
function to filter the array, and can use indexOf
to check whether time
field contains 15:00:00
or not.
E.g:
var json = [{
"name": "zack",
"message": "hello",
"time": "US 15:00:00"
},
{
"name": "zack",
"message": "hello",
"time": "US 00:00:00"
}
];
var resultObj = json.filter(item=>item.time.indexOf("15:00:00") !== -1);
console.log(resultObj);
var arr = [{
"name": "zack",
"message": "hello",
"time": "US 15:00:00"
}, {
"name": "zack",
"message": "hello",
"time": "US 00:00:00"
}]
for (var i = 0; i < arr.length; i++) {
var time = (arr[i].time.split('US '))[1];
console.log(time);
}
You can use array#filter function. It will return a new array with matched element. If the length of new array is 0 then no match was found
var myJson = [{
"name": "zack",
"message": "hello",
"time": "US 15:00:00"
},
{
"name": "zack",
"message": "hello",
"time": "US 00:00:00"
}
]
var m = myJson.filter(function(item) {
return item.time === "US 15:00:00"
})
console.log(m)
findIndex can also be used to find if it contains the value. If the value is -1 it mean the json array does not contain any object that match the criteria
var myJson = [{
"name": "zack",
"message": "hello",
"time": "US 15:00:00"
},
{
"name": "zack",
"message": "hello",
"time": "US 00:00:00"
}
]
var m = myJson.findIndex(function(item) {
return item.time === "US 15:00:00"
});
console.log(m)
本文标签: javascriptFind if a JSON Value contains certain textStack Overflow
版权声明:本文标题:javascript - Find if a JSON Value contains certain text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744834976a2627575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论