0

i have a problem with adding new values to existing objects when they have equal values. My point is to add new value to existing object, but when object value is equal with other postion object value then they have to get same new value, but otherwise object get just a value.

var array = [
    {"first": [10, 20], "last": [40, 50]},
    {"first": [60, 22], "last": [10, 20]},
    {"first": [40, 50], "last": [60, 22]},
    {"first": [40, 50], "last": [44, 33]}
];

for (var i = 0; i < (array.length); i++) {
    for (var j = 0; j < (array.length); j++) {
        if (array[i].first[0] == array[j].last[0] && array[i].first[1] == array[j].last[1]) {
            /* I'm not sure, what to do here
               but I want to add same values to equal objects
            */
            array[i].first.push(i);
            array[j].first.push(i);
        }
    }
}

I want to get that output:

var array = [
    {"first": [10, 20, 1], "last": [40, 50, 2]},
    {"first": [60, 22, 3], "last": [10, 20, 1]},
    {"first": [40, 50, 2], "last": [60, 22, 3]},
    {"first": [40, 50, 2], "last": [44, 33, 4]}
];

last number are new values,but im not sure how do get that output, i think maybe one way is to save equal objects positions and then doing something with them , but im not sure, maybe can somebody give me advice?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
user3699711
  • 49
  • 1
  • 8
  • 3
    *"...but when object value is equal with other postion object value then they have to get same new value, but otherwise object get just a value..."* Huh? – T.J. Crowder Mar 04 '15 at 14:19
  • I'm with @T.J.Crowder on this one. I *think* you are saying if you push a value into an array that contains the values `[10,20]`, you want all other arrays with the same `[10,20]` to get the same values pushed onto it. Is that anywhere near correct? – Matt Burland Mar 04 '15 at 14:21
  • if array[i].first[0] == array[j].last[0] the are equals the i want to add same value (exaple if they are equal, then add '1' to both and so on)? good? or not? – user3699711 Mar 04 '15 at 14:21
  • exactly Matt Burland – user3699711 Mar 04 '15 at 14:23
  • That doesn't explain the `2` on the first `last` array in your example output. – T.J. Crowder Mar 04 '15 at 14:23
  • there are "2" couse if it is not equal value it gets new value and if somewhere is the same value then the other get also "2" but if not then it gets also new value – user3699711 Mar 04 '15 at 14:29
  • This feels like it might be an `xy` problem, because it seems like a rather strange thing to do. If you could add more context to your question somebody might be able to suggest a better way. To you exact problem, for the array you are pushing to, you would need to loop over the rest of the array and compare the contents of the `first` and `last` arrays. If you find a match, push the new value to that array too. Presumably you could skip items that already have 3 values. – Matt Burland Mar 04 '15 at 14:29
  • i think the same way Matt Burland, but im not sure how to ask that question :( – user3699711 Mar 04 '15 at 14:33

1 Answers1

0

As in comments to Your question, I also find this problem quite strange. But... I think this is what You seek:

    <script>
        var array = [
            {"first": [10, 20], "last": [40, 50]},
            {"first": [60, 22], "last": [10, 20]},
            {"first": [40, 50], "last": [60, 22]},
            {"first": [40, 50], "last": [44, 33]}
        ];
        // This variable will hold unique subarrays. 
        // Index of it will be the number added to each subarray.
        var matches = new Array();
        var check_it = function(matches,subarr)
        {
            // Finding index of subarray in matches
            var index = matches.map( /./.test, RegExp( "^"+subarr+"$"  )).indexOf(true); 
            // If this subarray is new
            if(index === -1)
            { 
               // Add copy of it to matches
               matches.push([subarr[0],subarr[1]]);
               // Assing index to it
               subarr.push(matches.length-1);
            }
            // If this subarray exist in matches
            else
            {
                // Add the index of first match to it
                subarr.push(index);
            }
        }
        for (var i = 0; i < (array.length); i++) {
            // For each subarray do the checking.
            check_it(matches,array[i].first);
            check_it(matches,array[i].last);
        }
        console.log(array);
    </script>

From here I get code to find index of array in array.

Community
  • 1
  • 1
T.Z.
  • 2,092
  • 1
  • 24
  • 39
  • TNX, yes, thats the correct way :) but can you add some comments to your code, like what is happeing in the code – user3699711 Mar 04 '15 at 14:47