简体   繁体   English

如何在javascript中向相同的对象键添加相同的id值

[英]How to add same id value to equal object keys in javascript

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. 我的观点是为现有对象添加新值,但是当对象值与其他postion对象值相等时,它们必须获得相同的新值,否则对象只获得一个值。

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? 最后一个数字是新值,但我不知道如何获得该输出,我想也许一种方法是保存相等的对象位置,然后用它们做一些事情,但我不确定,也许有人可以给我建议吗?

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 . 从这里我得到代码来找到数组中的数组索引

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

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