1

Currently i have below Array of Objects

obj = [
    {
        "id":28,
        cities: [
            {
                cityTypes: "AA",
                citySource: "sdsf"
            },
            {
                cityTypes: "BB",
                citySource: "sdsgf"
            },
            {
                cityTypes: "CC",
                citySource: "fgsdfgd"
            }
        ]
    }, 
    {
        "id":56,
        cities: [
            {
                cityTypes: "DD",
                citySource: "sdsf"
            },
            {
                cityTypes: "EE",
                citySource: "sdsgf"
            },
            {
                cityTypes: "FF",
                citySource: "fgsdfgd"
            }
        ]
    }, 
    {
        "id":89,
        cities: [
            {
                cityTypes: "GG",
                citySource: "sdsf"
            },
            {
                cityTypes: "HH",
                citySource: "sdsgf"
            },
            {
                cityTypes: "II",
                citySource: "fgsdfgd"
            }
        ]
    }
]

I need to search cityTypes of specific value is present in the whole Object.

For suppose, i need to search cityTypes = BB

If BB is present in the whole object, return true

If BB is not preset, return false.

This is what i have tried, which does not seem to work.

for(let k=0; k<obj.length; k++){
    if(obj[k].cities){
        let cityObj = obj[k].cities;
        for(let city in cityObj){
            city.cityTypes !== "BB" ? "true" : "false"
        }
    }
}

What is the proper way to achieive this?

0

6 Answers 6

1

You might use a .some inside another .some:

const obj=[{"id":28,cities:[{cityTypes:"AA",citySource:"sdsf"},{cityTypes:"BB",citySource:"sdsgf"},{cityTypes:"CC",citySource:"fgsdfgd"}]},{"id":56,cities:[{cityTypes:"DD",citySource:"sdsf"},{cityTypes:"EE",citySource:"sdsgf"},{cityTypes:"FF",citySource:"fgsdfgd"}]},{"id":89,cities:[{cityTypes:"GG",citySource:"sdsf"},{cityTypes:"HH",citySource:"sdsgf"},{cityTypes:"II",citySource:"fgsdfgd"}]}];

console.log(
  obj.some(({ cities }) => cities.some(({ cityTypes }) => cityTypes === 'BB'))
);
console.log(
  obj.some(({ cities }) => cities.some(({ cityTypes }) => cityTypes === 'foobar'))
);

Sign up to request clarification or add additional context in comments.

Comments

1

Your existing code do not work because of several reason:

  1. You need to do let city of cityObj since you are accessing property of city as city.cityTypes inside the inner loop so using of will give you each object in city variable.
  2. You need to actually check if the match is found or not using a if condition. If a match is found with the cityTypes you expect then break the inner loop. Also break the outer loop if the match is found.

var obj = [{
    "id": 28,
    cities: [{
        cityTypes: "AA",
        citySource: "sdsf"
      },
      {
        cityTypes: "BB",
        citySource: "sdsgf"
      },
      {
        cityTypes: "CC",
        citySource: "fgsdfgd"
      }
    ]
  },
  {
    "id": 56,
    cities: [{
        cityTypes: "DD",
        citySource: "sdsf"
      },
      {
        cityTypes: "EE",
        citySource: "sdsgf"
      },
      {
        cityTypes: "FF",
        citySource: "fgsdfgd"
      }
    ]
  },
  {
    "id": 89,
    cities: [{
        cityTypes: "GG",
        citySource: "sdsf"
      },
      {
        cityTypes: "HH",
        citySource: "sdsgf"
      },
      {
        cityTypes: "II",
        citySource: "fgsdfgd"
      }
    ]
  }
]
var found = false;
for (let k = 0; k < obj.length; k++) {
  if (obj[k].cities) {
    let cityObj = obj[k].cities;
    for (let city of cityObj) {
      found = city.cityTypes === "BB";
      if (found) {
        break;
      }
    }
  }
  if (found) {
    break;
  }
}
console.log(found);

Comments

0

Using a double reduce should work

var obj = [{
    "id": 28,
    cities: [{
        cityTypes: "AA",
        citySource: "sdsf"
      },
      {
        cityTypes: "BB",
        citySource: "sdsgf"
      },
      {
        cityTypes: "CC",
        citySource: "fgsdfgd"
      }
    ]
  },
  {
    "id": 56,
    cities: [{
        cityTypes: "DD",
        citySource: "sdsf"
      },
      {
        cityTypes: "EE",
        citySource: "sdsgf"
      },
      {
        cityTypes: "FF",
        citySource: "fgsdfgd"
      }
    ]
  },
  {
    "id": 89,
    cities: [{
        cityTypes: "GG",
        citySource: "sdsf"
      },
      {
        cityTypes: "HH",
        citySource: "sdsgf"
      },
      {
        cityTypes: "II",
        citySource: "fgsdfgd"
      }
    ]
  }
]
var cityTypes1 = 'BB';
console.log(obj.reduce((total, cur) => 
    total||cur.cities.reduce((total, cur) => 
        total||cur.cityTypes === cityTypes1, false), 
    false))

Comments

0

This short method returns true if at least one of city has required type:

function findMyCode(code) {
   return obj.some((d) => {
       return d.cities.some((city) => {
           return city.cityTypes === code;
       });
   });
}

Or you can try use lodash library.

So you can use it:

var hasType = findMyCode('BB'); // returns true/false

Comments

0

Give a try with below code.

var result = false;
for(let k=0; k<obj.length; k++){
    if(obj[k].cities){
        let cityObj = obj[k].cities;
        for(let city in cityObj){
            if(cityObj[city].cityTypes == "BB"){
                result = true;
            }
        }
    }
}

return result or print console log.

Comments

0
var filtered = obj.filter((o) => {
    var found=false;
    for(let i=0;  i<o.cities.length; i++) {
        let city = o.cities[i];
        if(city.cityTypes === "BB"){
            found=true;
            break;
        }
    }
    return found;
})
console.log(filtered)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.