最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find if a JSON Value contains certain text - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 1

As 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)

发布评论

评论列表(0)

  1. 暂无评论