如何在javascript中向相等的对象键添加相同的id值
当现有对象的值相等时,我在向现有对象添加新值时遇到问题。我的观点是向现有对象添加新值,但当对象值与其他位置对象值相等时,它们必须获得相同的新值,否则对象只能获得一个值如何在javascript中向相等的对象键添加相同的id值,javascript,Javascript,当现有对象的值相等时,我在向现有对象添加新值时遇到问题。我的观点是向现有对象添加新值,但当对象值与其他位置对象值相等时,它们必须获得相同的新值,否则对象只能获得一个值 var array = [ {"first": [10, 20], "last": [40, 50]}, {"first": [60, 22], "last": [10, 20]}, {"first": [40, 50], "last": [60, 22]}, {"first": [40, 50],
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);
}
}
}
最后一个数字是新的值,但我不确定如何获得输出,我想可能有一种方法是保存相等的对象位置,然后对它们进行处理,但我不确定,也许有人能给我一些建议吗?在对您的问题的评论中,我也发现这个问题很奇怪。但是我想这就是你想要的:
<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>
变量数组=[
{“第一个”:[10,20],“最后一个”:[40,50]},
{“第一个”:[60,22],“最后一个”:[10,20]},
{“第一个”:[40,50],“最后一个”:[60,22]},
{“第一个”:[40,50],“最后一个”:[44,33]}
];
//此变量将保存唯一的子数组。
//它的索引将是添加到每个子阵列的编号。
var matches=新数组();
var check_it=功能(匹配,子R)
{
//在匹配中查找子数组索引
var index=matches.map(/./.test,RegExp(“^”+subar+“$”).indexOf(true);
//如果此子阵列是新的
如果(索引==-1)
{
//将其副本添加到匹配项
匹配。推送([SubAr[0],SubAr[1]]);
//将索引分配给它
子推力(匹配长度-1);
}
//如果此子阵列存在于匹配项中
其他的
{
//将第一个匹配项的索引添加到其中
子推力(指数);
}
}
对于(变量i=0;i<(array.length);i++){
//对每个子阵列执行检查。
检查它(匹配,数组[i]。首先);
检查它(匹配,数组[i].last);
}
console.log(数组);
从这里我可以找到代码。“…但是当对象值与其他位置对象值相等时,它们必须得到相同的新值,否则对象只能得到一个值…”嗯?我在这一点上与@T.J.Crowder一致。我认为您的意思是,如果您将一个值推入一个包含值
[10,20]
的数组,那么您希望所有具有相同[10,20]
的其他数组都将相同的值推入该数组。这几乎正确吗?如果数组[i]。第一个[0]==数组[j]。最后一个[0]等于我要添加的相同值(例如,如果它们相等,则将“1”添加到两者,依此类推)?好吗?或者不是?确切地说,Matt Burland没有解释示例输出中第一个最后一个数组上的2
。TNX,是的,这是正确的方法:)但是你能给代码添加一些注释吗,比如代码中发生了什么
<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>