简体   繁体   English

如何获取对象内部嵌套数组的值?

[英]How to get a value of nested array inside object?

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. 找到值[“ 320”]并返回该对象。

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)}

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. 奇怪的是, models某些条目是数组,其他条目是对象,而另一些条目是简单的字符串。

It's also odd that you're being asked to find ["350"] . 要求您查找["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 . 而且奇怪的是,有一个条目中的cars ,它没有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. 假设它应该是“ any”,那么您需要考虑到以下事实: models中的某些条目是数组,而其他不是。

 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. 这不能处理非数组对象models中的条目。 It's unclear what property in the object one is supposed to look at. 目前尚不清楚对象应该看什么属性。

The problem seems quite odd overall. 总的来说,这个问题看起来很奇怪。

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: 在进行比较之前,必须检查内部项目是否为数组,然后可以使用Array.prototype.includes()检查内部数组是否也包含您的值:

 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')); 

您能找到的最简单,最短的答案。

myObj.cars.find(i => i.models.find( j=>Array.isArray(j) && j.includes("320")) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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