Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

javascript - How to check if a value exists in an array in an object array?

I have an object in which extended address information (street name, zip code, house number, etc.). I need to check for the existence of a street, a house, and if they do not exist, display an error. The array is below:

const obj = {
    "address_components": [
      {
        "long_name": "10",
        "short_name": "100",
        "types": [
          "street_number"
        ]
      },
      {
        "long_name": "Engelsa street",
        "short_name": "Engelsa street",
        "types": [
          "route"
        ]
      },
      {
        "long_name": "Saint-Petersburg",
        "short_name": "SPB",
        "types": [
          "locality",
          "political"
        ]
      },
      {
        "long_name": "Russia",
        "short_name": "RU",
        "types": [
          "country",
          "political"
        ]
      },
      {
        "long_name": "194017",
        "short_name": "194017",
        "types": [
          "postal_code"
        ]
      }
    ],
    "formatted_address": "Engelsa street, 100, Saint-Petersburg, Russia, 194017",
  } 
question from:https://stackoverflow.com/questions/65641135/how-to-check-if-a-value-exists-in-an-array-in-an-object-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can search the address components array for some element entry that has a types array with every value you want it to contain.

obj.address_components.some(({ types }) =>
  types.every((type) => [/* list of keys */].includes(type))
);

const obj = {
    address_components: [
      {
        long_name: "10",
        short_name: "100",
        types: ["street_number"]
      },
      {
        long_name: "Engelsa street",
        short_name: "Engelsa street",
        types: ["route"]
      },
      {
        long_name: "Saint-Petersburg",
        short_name: "SPB",
        types: ["locality", "political"]
      },
      {
        long_name: "Russia",
        short_name: "RU",
        types: ["country", "political"]
      },
      {
        long_name: "194017",
        short_name: "194017",
        types: ["postal_code"]
      }
    ],
    formatted_address: "Engelsa street, 100, Saint-Petersburg, Russia, 194017"
  };

const res = obj.address_components.some(({ types }) => types.every(type => ['country', 'locality'].includes(type)));
const res2 = obj.address_components.some(({ types }) => types.every(type => ['country', 'political'].includes(type)));

console.log("'country', 'locality'", res);
console.log("'country', 'political'", res2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...