简体   繁体   English

比较 JavaScript 中 2 个不同的嵌套数组

[英]Comparing between 2 different nested arrays in JavaScript

I've been on this for a while but haven't been able to arrive at my desired result.我已经有一段时间了,但未能达到我想要的结果。 I'd like to compare items in two arrays based on their indexes.我想根据它们的索引比较两个数组中的项目。 Something like this:像这样的东西:

const first = 0;
const second = 1;
const result = []; // should equal (false, true, false)

const final = [[[2,3],[1,1]],[[4,2],[3,0]],[[0,3],[1,1]]];

for (let i = 0; i < final.length; i++) {
    for (let j = 0; j < final[i].length; j++){
//Comparison between ((2 and 3) && (1 and 1)) with a single result.
//Should also compare between ((4 and 2) && (3 and 0)) and many other nested arrays
//This logic is not the right one as it can't do the comparison as intended.
      if (final[i][j][first] > final[i][j][second]) {
result.push(true);
    } else result.push(false);
  }


I hope this is understood, enough.我希望这被理解,足够了。 There is a very similar problem like this one.有一个非常相似的问题。 I don't know if it's right to post it here or open another ask a question, but they're both similar.我不知道在这里发布或打开另一个问题是否正确,但它们都很相似。

Thanks very much.非常感谢。

You can try it :你可以试试 :

 const first = 0; const second = 1; const final = [[[2,3],[1,1]],[[4,2],[3,0]],[[0,3],[1,1]]]; const result = final.map(([e1, e2]) => (e1[first] > e1[second] && e2[first] > e2[second])) console.log(result) // with non arrow function function compareArr(arr, first, second) { return arr.map(function (ele) { return ele[0][first] > ele[0][second] && ele[1][first] > ele[1][second] }) } console.log(compareArr(final, first, second)) // with non map function : function compareArr1(arr, first, second) { let result1 = [] for(let i = 0; i < arr.length; i++) { result1[i] = true for(let j = 0; j < arr[i].length; j++) { if(!(arr[i][j][first] > arr[i][j][second])) result1[i] = false } } return result1 } console.log(compareArr1(final, first, second))

UPDATE : edit with suggest from @ yes sir更新:编辑@是先生的建议

Update : about ([e1,e2]) :更新:关于([e1,e2])

normal : Array.map((ele) => .....) with structure of ele is [a, b]正常: Array.map((ele) => .....)结构为 ele 是[a, b]

It can use same as : Array.map(([a, b]) => .....)它可以使用相同的: Array.map(([a, b]) => .....)

Document of it : Destructuring assignment它的文档: 解构分配

Xupitan used functional programming, which is the better approach in my opinion, but I tried to extend your code. Xupitan 使用函数式编程,在我看来这是更好的方法,但我尝试扩展您的代码。

what you previously lacked was that you weren't keeping tracking of the first array, you were comparing every nested array, this approach is also valid but you need to then compare each results eg, result[i] && result[i+1].您之前缺少的是您没有跟踪第一个数组,您正在比较每个嵌套数组,这种方法也是有效的,但是您需要然后比较每个结果,例如 result[i] && result[i+1] .

 const first = 0; const second = 1; const result = []; // should equal (true, true, false) const final = [[[2,3],[1,1]],[[4,2],[3,0]],[[0,3],[1,1]]]; let k=0 for (let i = 0; i < final.length; i++) { for (let j = 0; j < final[i].length; j++){ //Comparison between ((2 and 3) && (1 and 1)) with a single result. //Should also compare between ((4 and 2) && (3 and 0)) and many other nested arrays //This logic is not the right one as it can't do the comparison as intended. if (final[i][j][first] > final[i][j][second]) { if(k > 0){ result[i] = true } result[i] = true }else{ //first false exit result[i] = false; break; } k++ } k=0 } console.log(result)

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

相关问题 JavaScript:比较对象的 arrays 与嵌套数组 object - JavaScript: Comparing arrays of objects with nested array of object 比较 2 个在 Javascript 中不起作用的数组的值 - Comparing between values of 2 arrays not working in Javascript 比较JavaScript中两个数组的不同值 - Comparing two arrays in Javascript for different values 比较Angularjs中2个嵌套数组之间的对象并显示相应的匹配数据 - Comparing objects between 2 nested arrays in Angularjs and display respective matching data 将数组与嵌套循环进行比较 - Comparing Arrays with nested loops 比较 2 个嵌套对象数组,同时忽略 Javascript 中的某些属性 - Comparing 2 arrays of nested objects while ignoring some properties in Javascript 比较嵌套的 object arrays 和存在的返回值 JavaScript, React - Comparing nested object arrays and returning values that exist JavaScript, React Javascript在嵌套的Function()之间传递数组 - Javascript passing arrays between nested Functions() 在php和javascript中比较数组会返回2个不同的值。 为什么? - comparing arrays in php and in javascript return 2 different values. Why? 比较两个 arrays 的不同值并在 JavaScript 中返回一个过滤数组 - comparing different values of two arrays and returning one filtered array in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM