0

I have two 2-D arrays like:

var data_consumed_dollars = [[1356998400000, 0.030380282850000006], [1357002000000, 0.0395505288], [1357005600000, 0.04084797307500001]];

var data_solargeneration_dollars = [[1356998400000, 0.030972182850000006], [1357178400000, 0], [1357182000000, 0], [1357185600000, 0], [1357189200000, 0], [1356998400000, 0.030380282850000006]];

I need to find the common values in these two arrays. I have tried this so far

var ab = [];
var data_consumed_dollars_common = [];
for (var i = 0; i < data_consumed_dollars.length; i++) {
    var a, b;
    if (parseFloat(data_solargeneration_dollars[i][0]) > parseFloat(data_consumed_dollars[i][0])) {
        a = data_consumed_dollars[i][0];
        ab.push(data_consumed_dollars[i][0]);
    }
    else a = data_solargeneration_dollars[i][0];
    b = data_solargeneration_dollars[i][1];

    data_consumed_dollars_common.push([ a, b ]);
}

But data_consumed_dollars_common is not returning the desired result.

2
  • isnt an intersection a set of elements that are equal? Commented Nov 13, 2014 at 10:13
  • @MaxBumaye: Yes absolutely and that's what I'm trying to achieve. The first element of the first array and the last element of the second array are equal. I need this value to be stored in the resulting array. Commented Nov 13, 2014 at 10:35

2 Answers 2

2

For your code I assume that what you are seeking is to get the greater value for each id inside your both arrays.

That's not exactly the intersection of them. The intersection of two groups is a group with the elements that are present on both original groups.

First of all, and using Javascript, you'll probably go for a much easier run if you adopt a data structure based on JSON-like objects more like:

var data_consumed_dollars = { "1356998400000": 0.030380282850000006, "1357002000000": 0.0395505288, "1357005600000": 0.04084797307500001 };

var data_solargeneration_dollars = { "1357174800000": 0, "1357178400000": 0, "1357182000000": 0, "1357185600000": 0, "1357189200000": 0, "1356998400000": 0.030380282850000006 };

However, this could be arguably, so just keep it in mind as an option.

I think, that probably the thing that's keeping your code from working as you want is that you are missing {} after the else, so at each iteration, the value you push to b is always data_solargeneration_dollars[i][1];

Sadly this can be only an assumption, as I'm not actually sure what you are asking for...

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

3 Comments

thats what I was also thinking of, but got caught up with thinking about what exactly is beeing intersected (nothing) -> the author of this question should mark this as correct
@MaxBumaye: Yes absolutely and that's what I'm trying to achieve. The first element of the first array and the last element of the second array are equal. I need this value to be stored in the resulting array.
@sanki: If you're seeking for an algorithm to actually intersect two arrays you should check stackoverflow.com/questions/1885557/…, as Dexa posted as a comment to your question.
0

This is the way I tried to settle the issue.

var data_consumed_dollars = [[1356998400000, 0.030380282850000006], [1357002000000, 0.0395505288], [1357005600000, 0.04084797307500001]];

var data_solargeneration_dollars = [[1356998400000, 0.030972182850000006], [1357178400000, 0], [1357182000000, 0], [1357185600000, 0], [1357189200000, 0], [1356998400000, 0.030380282850000006]];

var matched_elements = [];

for(var x = 0; x < data_consumed_dollars.length; x++){
    for(var y = 0; y < data_solargeneration_dollars.length; y++){
      if(data_consumed_dollars[x][0] == data_solargeneration_dollars[y][0] && data_consumed_dollars[x][1] == data_solargeneration_dollars[y][1]){
  matched_elements.push([data_consumed_dollars[x][0],data_consumed_dollars[x][1]]);
      }
    }
  }
console.log(matched_elements);

DEMO FIDDLE

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.