简体   繁体   中英

How do I check an object property value that the value is a string with a space

I feel like this should be straight forward but I can not find anything on this. I have an array of objects and I want to filter it based off of their values.

let arr = [
  {
    name : "Justin",
    location : "This place"
  },
  {
    name : "Steve",
    location : "That place"
  }
]

This works and returns the object with the name Justin

arr.filter(x => x.name == "Justin");

However when I run this all I get back is an empty array

arr.filter(x => x.location == "This place");

It seems to only happen when my string has a space in it.

 let arr = [ { name : "Justin", location : "This place" }, { name : "Steve", location : "That place" } ] console.log(arr.filter(x => x.name == "Justin")); console.log(arr.filter(x => x.location == "This place")); 

The problem was never with my filter after all. The CSV file that I had built all my objects off of had put an extra set of quotations around my strings... So instead of "East Lyme" the value was ""East Lyme"".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM