比较两个对象数组的重复对象,并在 JavaScript 中不重复时将其推送
Comparing two arrays of objects for duplicate objects and push it when it's not a duplicate in JavaScript
这个问题可能与我几个小时前问的 太相似了,但我正在努力解决的问题实际上比我最初想象的要复杂。我不知道在 JavaScript 中没有两个对象可以被认为是相同的,即使两个对象具有相同的属性和值集也是如此。
我在 JavaScript 中有两个像这样的对象数组。
var from = [
{city: "seoul", country: "korea"},
{city: "tokyo", country: "japan"},
{city: "beijing", country: "china"},
{city: "new york", country: "usa"}
];
var to = [
{city: "seoul", country: "korea"},
{city: "tokyo", country: "japan"},
{city: "toronto", country: "canada"}
];
我想做的是检查"from"数组中的任何对象是否已经在"to"数组中,只有当它不在 "to" 数组中,我只想将 "from" 数组中的一个对象添加到 "to" 数组,即使 "from" 数组中还有其他对象不在 "to" 数组中。换句话说,我想在将对象推入 "to" 数组后立即跳出循环。
最后,我希望 "to" 数组看起来像这样。
var target = {
{city: "seoul", country: "korea"},
{city: "tokyo", country: "japan"},
{city: "toronto", country: "canada"},
{city: "beijing", country: "china"}
};
这是我想出的一个函数来实现这个效果。
function ruthere (source, target) {
for (var i = 0; i < target.length; i++) {
for (var j = 0; j < source.length; j++) {
if (target[i].city == source[j].city) {
console.log("Already there");
} else {
target.push(source[j]);
i = target.length-1;
break;
}
}
}
}
这是我目前为止达到我想要的结果,但这仍然没有达到我想要的效果。
*edit: 我只需要检查一个对象的名称 属性 的值是否相同。国家 属性 的值不必相同。
我会考虑使用下划线来帮助您实现这一点:
Underscore.js
这个答案可能是一个非常有用的答案:
您的代码有一些问题:您确实需要一个嵌套循环,但不是因为您在这里这样做的原因。如果目标数组中的城市与源数组中的城市不完全相同,那么有些城市可能永远不会被推送到目标数组。城市也可以具有相同的名称但位于不同的国家/地区。
假设您可以使用 ES5 功能:
function ruthere(source, target) {
for (var i = 0; i < source.length; i++) {
var srcObj = source[i];
var tarObj = target.find(function (obj) {
return obj.city === srcObj.city && obj.country === srcObj.country;
});
if (!tarObj) {
target.push(srcObj);
break;
}
}
return target;
}
编辑:我错了,Array.prototype.find
是 ES6,不是 ES5。这是 ES3 版本:
function ruthere(source, target) {
for (var i = 0; i < source.length; i++) {
var srcObj = source[i];
var tarObj;
for (var j = 0; j < target.length; j++) {
var obj = target[j];
if (srcObj.city === obj.city && srcObj.country === obj.country) {
tarObj = obj;
break;
}
}
if (!tarObj) {
target.push(srcObj);
break;
}
}
return target;
}
只需使用;
var from = [{
city: "seoul",
country: "korea"
}, {
city: "tokyo",
country: "japan"
}, {
city: "beijing",
country: "china"
}, {
city: "new york",
country: "usa"
}];
var to = [{
city: "seoul",
country: "korea"
}, {
city: "tokyo",
country: "japan"
}, {
city: "toronto",
country: "canada"
}];
function ruthere(source, target) {
for (var i = 0; i < source.length; i++) {
if (source[i].city !== target[i].city) {
target.push(source[i])
}
}
for (var i = 0; i < target.length; i++) {
$(".city").append((i + 1) + " " + target[i].city + "<br>")
}
}
ruthere(from, to)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="city"></div>
这个问题可能与我几个小时前问的
我在 JavaScript 中有两个像这样的对象数组。
var from = [
{city: "seoul", country: "korea"},
{city: "tokyo", country: "japan"},
{city: "beijing", country: "china"},
{city: "new york", country: "usa"}
];
var to = [
{city: "seoul", country: "korea"},
{city: "tokyo", country: "japan"},
{city: "toronto", country: "canada"}
];
我想做的是检查"from"数组中的任何对象是否已经在"to"数组中,只有当它不在 "to" 数组中,我只想将 "from" 数组中的一个对象添加到 "to" 数组,即使 "from" 数组中还有其他对象不在 "to" 数组中。换句话说,我想在将对象推入 "to" 数组后立即跳出循环。
最后,我希望 "to" 数组看起来像这样。
var target = {
{city: "seoul", country: "korea"},
{city: "tokyo", country: "japan"},
{city: "toronto", country: "canada"},
{city: "beijing", country: "china"}
};
这是我想出的一个函数来实现这个效果。
function ruthere (source, target) {
for (var i = 0; i < target.length; i++) {
for (var j = 0; j < source.length; j++) {
if (target[i].city == source[j].city) {
console.log("Already there");
} else {
target.push(source[j]);
i = target.length-1;
break;
}
}
}
}
这是我目前为止达到我想要的结果,但这仍然没有达到我想要的效果。
*edit: 我只需要检查一个对象的名称 属性 的值是否相同。国家 属性 的值不必相同。
我会考虑使用下划线来帮助您实现这一点: Underscore.js
这个答案可能是一个非常有用的答案:
您的代码有一些问题:您确实需要一个嵌套循环,但不是因为您在这里这样做的原因。如果目标数组中的城市与源数组中的城市不完全相同,那么有些城市可能永远不会被推送到目标数组。城市也可以具有相同的名称但位于不同的国家/地区。
假设您可以使用 ES5 功能:
function ruthere(source, target) {
for (var i = 0; i < source.length; i++) {
var srcObj = source[i];
var tarObj = target.find(function (obj) {
return obj.city === srcObj.city && obj.country === srcObj.country;
});
if (!tarObj) {
target.push(srcObj);
break;
}
}
return target;
}
编辑:我错了,Array.prototype.find
是 ES6,不是 ES5。这是 ES3 版本:
function ruthere(source, target) {
for (var i = 0; i < source.length; i++) {
var srcObj = source[i];
var tarObj;
for (var j = 0; j < target.length; j++) {
var obj = target[j];
if (srcObj.city === obj.city && srcObj.country === obj.country) {
tarObj = obj;
break;
}
}
if (!tarObj) {
target.push(srcObj);
break;
}
}
return target;
}
只需使用;
var from = [{
city: "seoul",
country: "korea"
}, {
city: "tokyo",
country: "japan"
}, {
city: "beijing",
country: "china"
}, {
city: "new york",
country: "usa"
}];
var to = [{
city: "seoul",
country: "korea"
}, {
city: "tokyo",
country: "japan"
}, {
city: "toronto",
country: "canada"
}];
function ruthere(source, target) {
for (var i = 0; i < source.length; i++) {
if (source[i].city !== target[i].city) {
target.push(source[i])
}
}
for (var i = 0; i < target.length; i++) {
$(".city").append((i + 1) + " " + target[i].city + "<br>")
}
}
ruthere(from, to)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="city"></div>