0

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.

3
  • const result = []; // should equal (true, true, false) !? with your condition : final[i][j][first] > final[i][j][second]) I think it's like : (false, true, false) Commented Jun 4, 2022 at 17:40
  • Oh, yeah, thanks. That's how it should be @Xupitan. Commented Jun 4, 2022 at 17:42
  • Gonna change it, now. Commented Jun 4, 2022 at 17:43

2 Answers 2

3

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]) :

normal : Array.map((ele) => .....) with structure of ele is [a, b]

It can use same as : Array.map(([a, b]) => .....)

Document of it : Destructuring assignment

Sign up to request clarification or add additional context in comments.

9 Comments

e1[first] > e1[second] && e2[first] > e2[second] instead of e1[first] > e1[second] && e1[first] > e1[second], i can't edit it.
oh sorry ! i'm lazy ^^!
Great! Thanks very much. But I'm trying to understand the arrow function, it seems hard, can you help to elaborate?
with arrow function, you have context is larger than function norma
you can read more in here : stackoverflow.com/questions/34361379/…
|
1

Xupitan used functional programming, which is the better approach in my opinion, but I tried to extend your code.

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 e.g., 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.