1

My value is inside an object and inside a nested array. The task is if value matches then it should return the whole object. Find the value ["320"] and return that object.

var myObj = {
  "name":"John",
  "age":30,
  "cars": [
    { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
    { "name":"BMW", "models":[ ["320"], "X3", "X5" ] },
    { "models":[{ "test": "abcd", key:[{bcc:'hello'}] } , "Panda"] }
  ]
}

This is what I've done

myObj.cars.find(i => i.models.find(j=>j===["320"]))

Expected result

{name: "BMW", models: Array(3)}
1565986223
  • 6,420
  • 2
  • 20
  • 33
  • It's very odd that `models` has some entries that are arrays and others that aren't. Is that really part of the problem you've been asked to solve? – T.J. Crowder Apr 15 '19 at 11:32

3 Answers3

1

You can't use === (or ==) to compare two different arrays. It will check to see if they're the same array, not equivalent arrays.

It's very odd that models has some entries that are arrays, others that are objects, and others that are simple strings.

It's also odd that you're being asked to find ["350"]. Does that mean you should find cars that matcy all of the models in that array? Or any of them? Doesn't really matter when the array has only one entry, but arrays by their nature can have multiple entries...

And it's odd that there's an entry in cars that doesn't have a name.

Assuming it should be "any," then you need to allow for the fact that some entries in models are arrays and others aren't.

var myObj = {
    "name":"John",
    "age":30,
    "cars": [
        { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
        { "name":"BMW", "models":[ ["320"], "X3", "X5" ] },
        { "models":[{ "test": "abcd", key:[{bcc:'hello'}] } , "Panda"] }
    ]
};

const modelsToFind = ["320"];
const result = myObj.cars.find(car =>
    car.models.find(model => {
        // If it's not an array, make it one
        if (!Array.isArray(model)) {
            model = [model];
        }
        // Does it have any of the models we want?
        return model.some(m => modelsToFind.includes(m));
    })
);
console.log(result);

That doesn't handle entries in models that are non-array objects. It's unclear what property in the object one is supposed to look at.

The problem seems quite odd overall.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You have to check if your inner items are arrays or not before doing the comparison, then you can use Array.prototype.includes() to check if the inner arrays also contain your value:

const myObj = {
    "name":"John",
    "age":30,
    "cars": [
        { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
        { "name":"BMW", "models":[ ["320"], "X3", "X5" ] },
        { "models":[{ "test": "abcd", key:[{bcc:'hello'}] } , "Panda"] }
    ]
};

const find = (obj, val) => obj.cars.find(i => i.models.find(j =>
  Array.isArray(j) ? j.includes(val) : j === val
));

console.log(find(myObj, '320'));
console.log(find(myObj, 'X3'));
jo_va
  • 13,504
  • 3
  • 23
  • 47
0

The easiest and the shortest answer you could find.

myObj.cars.find(i => i.models.find( j=>Array.isArray(j) && j.includes("320")) )
jo_va
  • 13,504
  • 3
  • 23
  • 47