Javascript 返回数组中数组对象的链接的有效且舒适的方法?

Javascript 返回数组中数组对象的链接的有效且舒适的方法?,javascript,Javascript,我有一个数组: let array = [{ 'a': 1, 'b': [{ 'c': 1, 'd': 2 }, { 'c': 3, 'd': 4 }] }, { 'a': 3, 'b': [{ 'c': 5, 'd': 6 }, { 'c': 7, 'd': 8 }] }, { 'a': 5,

我有一个数组:

let array = [{
    'a': 1,
    'b': [{
        'c': 1,
        'd': 2
    }, {
        'c': 3,
        'd': 4
    }]
}, {
    'a': 3,
    'b': [{
        'c': 5,
        'd': 6
    }, {
        'c': 7,
        'd': 8
    }]
}, {
    'a': 5,
    'b': [{
        'c': 9,
        'd': 10
    }, {
        'c': 11,
        'd': 12
    }]
}];
我需要找到属性c中包含值9的对象,并将其更改为88。我使用的代码如下:

const toFindValue = 9

const result = array.filter(f1 =>
                                f1.b.some(v =>
                                              v.c === toFindValue))
                    .map(f2 =>
                             f2.b.filter(f3 =>
                                             f3.c === toFindValue))
                    .flat(Infinity)[0];

result.c = 88;

array.forEach(v => console.log(v))
让数组=[{ "a":1,, “b”:[{ "c":1,, d:2 }, { "c":3,, d:4 }] }, { "a":3,, “b”:[{ “c”:5, d:6 }, { “c”:7, d:8 }] }, { “a”:5, “b”:[{ "c":9,, d:10 }, { "c":11,, d:12 }] }]; 常数toFindValue=9 const result=array.filterf1=> f1.b.somev=> v、 c==toFindValue .mapf2=> f2.b.filterf3=> f3.c==toFindValue .无穷大[0]; 结果:c=88; array.forEachv=>console.logv有些过滤器嵌套在一个过滤器中,而另一个过滤器嵌套在一个后跟flat的映射中,这有点复杂。我认为在b数组上迭代并重新分配对象的c属性(如果它等于toFindValue)会更容易:

让数组=[{ "a":1,, “b”:[{ "c":1,, d:2 }, { "c":3,, d:4 }] }, { "a":3,, “b”:[{ “c”:5, d:6 }, { “c”:7, d:8 }] }, { “a”:5, “b”:[{ "c":9,, d:10 }, { "c":11,, d:12 }] }]; 常数toFindValue=9; 外部: 对于数组的常数{b}{ 对于b的常数obj{ 如果对象c==toFindValue{ 对象c=88; 打破外部; } } } console.logarray 有些过滤器嵌套在一个过滤器中,而另一个过滤器嵌套在一个映射中,后跟一个平面,这有点复杂。我认为在b数组上迭代并重新分配对象的c属性(如果它等于toFindValue)会更容易:

让数组=[{ "a":1,, “b”:[{ "c":1,, d:2 }, { "c":3,, d:4 }] }, { "a":3,, “b”:[{ “c”:5, d:6 }, { “c”:7, d:8 }] }, { “a”:5, “b”:[{ "c":9,, d:10 }, { "c":11,, d:12 }] }]; 常数toFindValue=9; 外部: 对于数组的常数{b}{ 对于b的常数obj{ 如果对象c==toFindValue{ 对象c=88; 打破外部; } } }
console.logarray 如果您只想更改与findValue匹配的第一个值,Array.prototype.find可能是更好的方法:

array.findv=>v.b.somee=>e.c==toFindValue B .finde=>e.c===toFindValue .c=88
当然,您应该注意未定义的错误。

如果您只想更改与findValue匹配的第一个错误,那么Array.prototype.find可能是更好的方法:

array.findv=>v.b.somee=>e.c==toFindValue B .finde=>e.c===toFindValue .c=88
当然,您应该注意未定义的错误。

外键的两个循环,然后嵌套数组是我能想到的最有效的方法,应该比OP的5个循环更有效

作出以下假设:

b是存在于每个集合中的密钥 c是存在于b中且仅存在于b中的密钥 考虑到这一点,我添加了更多的值,其中c==9,它们都变为88。为保持这一清洁而分配的对象分解任务感到荣幸:

常量数组=getData 常数toFindValue=9; 常数toSetValue=88; array.forEach{b}=>{ b、 forEach{c},i=>{ 如果c==toFindValue b[i].c=toSetValue; } } array.forEachv=>console.logv /**************************************************************************/ 函数getData{ 返回[{ "a":1,, “b”:[{ "c":1,, d:2 }, { "c":3,, d:4 }] }, { "a":3,, “b”:[{ “c”:5, d:6 }, { “c”:7, d:8 }] }, { “a”:5, “b”:[{ "c":9,, d:10 }, { "c":11,, d:12 }] }, { “a”:7, “b”:[{ "c":9,, d:13 }, { "c":9,, "丁":14 }] } ]; }
作为控制台包装器{max height:100vh!important;}A两个外键循环,然后嵌套数组是我能想到的最有效的方法,它应该超过OP的5个循环

作出以下假设:

b是存在于每个集合中的密钥 c是存在于b中且仅存在于b中的密钥 考虑到这一点,我添加了更多的值,其中c==9,它们都变为88。为保持这一清洁而分配的对象分解任务感到荣幸:

常量数组=getData 常数toFindValue=9; 常数toSetValue=88; array.forEach{b}=>{ b、 forEach{c},i=>{ 如果c==toFindValue b[i].c=toSetValue; } } array.forEachv=>console.logv /**************************************************************************/ 函数getData{ 返回[{ "a":1,, “b”:[{ "c":1,, d:2 }, { "c":3,, d:4 }] }, { "a":3,, “b”:[{ “c”:5, d:6 }, { “c”:7, d:8 }] }, { “a”:5, “b”:[{ "c":9,, d:10 }, { "c":11,, d:12 }] }, { “a”:7, “b”:[{ "c":9,, d:13 }, { "c":9,, "丁":14 }] } ]; }
.as控制台包装器{max height:100vh!important;}在找到匹配项时,上面的将使用some和find在b上循环两次,因此看起来效率不高。此外,它只会更改c=9的第一个匹配项,如果b包含多个项目,如果c=9,它将忽略除第一个项目之外的其他项目。当找到匹配项时,上面将在b上循环两次,使用一些,然后使用find,所以它看起来效率不高。此外,它只会改变c=9的第一次出现,它b包含多个项目,有c=9,它会忽略它们,除了第一个。@deni5n:我想,对于未来的读者来说,值得一提的是,初始代码和接受的答案都假设有一个唯一的项目,有c=9。否则,它将跳过除第一个之外的匹配。@YevgenGorbunkov如果他们真的是读者,他们应该能够阅读最后一行,其中提到如果c=9不是唯一的,该怎么做,以及如何不跳过匹配:@deni5n:我想,值得一提的是,对于未来的读者来说,初始代码和接受的答案都假设有一个唯一的项,c=9。否则,它将跳过除第一个之外的匹配。@YevgenGorbunkov如果他们真的是读卡器,他们应该能够阅读最后一行,其中提到如果c=9不是唯一的,该怎么做,以及如何不跳过匹配:
const toFindValue = 9;
outer:
for (const { b } of array) {
  for (const obj of b) {
    if (obj.c === toFindValue) {
      obj.c = 88;
      break outer;
    }
  }
}