Artem Bakhmat
Artem Bakhmat

Reputation: 307

How to check if array has nested property with defined value

I have an array of complicated objects and arrays in javascript such as:

var array = [
    { "simpleProp": "some value" },
    { "booleanProp": false },
    {
        "arrayProp": [
            { "prop1": "value1" },
            {
                "prop2": {
                    "prop22": "value22",
                    "prop23": "value23"
                } 
            },
            { "prop3": "value3" },
            { "booleanProp": true }
        ]
    }
];

I have to know if there is a property with defined value in my array, such as:

   function some(array, property, value) {
        //some logic here
       // return boolean
    };

That is, for my source array the result of this:

var result = some(array, "booleanProp", true) - must be TRUE.

I tried to use lodash function _.some(), but it returns false for my array, it appears _.some() can't find deeply nested properties.

It would be very cool if the function may support complicated object as source, not only array.

I'd appreciate any help, thanks.

Upvotes: 1

Views: 2638

Answers (2)

Raj Chauhan
Raj Chauhan

Reputation: 61

Above Solution is great but it is not working for Array. So I've Modified it little bit and now it is working for both Arrays & normal properties. Even In Arrays element's placement can be anything.

const data = {
    "names": [
      {
        "name": {
          'homename': 'Raju',
          'academisName': 'Rajpal',
          'callingName': ['Raj', 'Rajpal', 'Raju']
        },
        "defaultName": "Raj"
      }]
  }

Code for Array:

const some = (object, property, value) => {
  return _.isArray(value) && _.isEqual(_.sortBy(object[property]), _.sortBy(value)) || object[property] === value || Object.keys(object).some(function (k) {
    return object[k] && typeof object[k] === 'object' && some(object[k], property, value);
  });
}

const data = {
  "names": [{
    "name": {
      'homename': 'Raju',
      'academisName': 'Rajpal',
      'callingName': ['Raj', 'Rajpal', 'Raju']
    },
    "defaultName": "Raj"
  }]
}
const some = (object, property, value) => {
  return _.isArray(value) && _.isEqual(_.sortBy(object[property]), _.sortBy(value)) || object[property] === value || Object.keys(object).some(function(k) {
    return object[k] && typeof object[k] === 'object' && some(object[k], property, value);
  });
}
console.log('Result 1', some(data, 'callingName', ["Raj", "Rajpal", "Raju"]));
console.log('Result 2', some(data, 'callingName', ["Rajpal", "Raj", "Raju"]));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.js"></script>

Note: that value.sort() will mutate the arrays so I've used _.sortBy(value), same for object[property]

 console.log(some(data, 'callingName',  ["Raj", "Rajpal", "Raju"]));
 console.log(some(data, 'callingName', ["Rajpal", "Raj", "Raju"]));

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386868

You could use an iterative and recursive approach by checking the actual object and if the value is an object iterate the object's keys.

function some(object, property, value) {
    return object[property] === value || Object.keys(object).some(function (k) {
         return object[k] && typeof object[k] === 'object' && some(object[k], property, value);
    });
}

var data = [{ simpleProp: "some value" }, { booleanProp: false }, { arrayProp: [{ prop1: "value1" }, { prop2: { prop22: "value22", prop23: "value23" } }, { prop3: "value3" }, { booleanProp: true }] }];

console.log(some(data, 'booleanProp', true)); // true
console.log(some(data, 'foo', 42));           // false

Upvotes: 5

Related Questions