简体   繁体   English

在嵌套数组中查找和编辑?

[英]Find and edit in nested array?

I have a nested array that changes dynamically.我有一个动态变化的嵌套数组。 I need to check if the array contains an object and then modify the object keeping the rest of the items the same.我需要检查数组是否包含一个对象,然后修改该对象,使其余项目保持不变。

Sample:样本:

var input = [
    ['a', 'b'],
    ['c', 'd'],
    ['e', ['f', 'g']],
    ['h', ['i', 'j']]
]

In this nested array, I would like to find f and change it to f-is-found在这个嵌套数组中,我想找到f并将其更改为f-is-found

var result = [
    ['a', 'b'],
    ['c', 'd'],
    ['e', ['f-is-found', 'g']],
    ['h', ['i', 'j']]
]

The problem is that f can change it's location in the array, so I can't use any hard coded ways of accessing the array.问题是f可以改变它在数组中的位置,所以我不能使用任何硬编码的方式来访问数组。

Help is appreciated, thank you感谢帮助,谢谢

You can use a recursive approach, by looping through every element in your array (and looping over every inner-array and so on...), and then check whether your current value is equal to f , if it is, you can modify it, otherwise, if the current value is an array, you can again search that array using the same function.您可以使用递归方法,通过遍历数组中的每个元素(并遍历每个内部数组等等...),然后检查您的当前值是否等于f ,如果是,您可以修改否则,如果当前值是一个数组,您可以使用相同的函数再次搜索该数组。

See example below:请参阅下面的示例:

 const input = [ ['a', 'b'], ['c', 'd'], ['e', ['f', 'g']], ['h', ['i', 'j']] ]; function findAndChange(search, toadd, arr) { arr.forEach((val, i) => { if (!Array.isArray(val)) { if (val === search) { arr[i] = val + toadd; } } else { findAndChange(search, toadd, val); } }); } findAndChange('f', '-is-found', input); console.log(input);

Not a big fan of reinventing the wheel.不太喜欢重新发明轮子。 We use object-scan for a lot of our data processing now and it's powerful once you wrap your head around how to use it.我们现在使用对象扫描进行大量数据处理,一旦您了解如何使用它,它就会变得强大。 Here is how you could answer your questions.以下是您可以如何回答您的问题。

Note that the input is mutated in place.请注意,输入在原地发生了变化。 If you expected multiple instances of 'f' , you could simply change the abort to false and change the rnt to count to see how many instances were replaces.如果您希望有多个'f'实例,您可以简单地将abort更改为false并将rnt更改为count以查看替换了多少个实例。

 // const objectScan = require('object-scan'); const input = [ ['a', 'b'], ['c', 'd'], ['e', ['f', 'g']], ['h', ['i', 'j']] ]; const modify = (searchValue, replaceValue, data) => objectScan(['**'], { rtn: 'bool', abort: true, filterFn: ({ value, parent, property }) => { if (value === searchValue) { parent[property] = replaceValue; return true; } return false; } })(input); const r = modify('f', 'f-is-found', input); console.log(r); // true iff replace was executed // => true console.log(input); /* => [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e', [ 'f-is-found', 'g' ] ], [ 'h', [ 'i', 'j' ] ] ] */
 .as-console-wrapper {max-height: 100% !important; top: 0}
 <script src="https://bundle.run/object-scan@13.8.0"></script>

Disclaimer : I'm the author of object-scan免责声明:我是对象扫描的作者

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

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