英文:
How to compare two equal value in two object
问题
Here's the translated code part you provided:
我想要比较这两个数组,以获取不在食物数组中存在的水果的id和名称,并将结果保存在新数组中。
例如,在这里,橙子在食物数组中存在,结果应该存储不在食物中存在的水果的id和名称(苹果和樱桃)。
const fruits = [{id: '1', name: 'Apple'},
{id: '2', name: 'Orange'},
{id: '3', name: 'Cherry'}];
const food=[{id: '1', creation_date: '2023-05-13 09:46:25', created_by: '1'},
{id: '1', food_name: 'Orange'},
{id: '2', food_name: 'Bread'},
{id: '3', food_name: 'Cheese'},
{id: '4', food_name: 'Milk'},
{id: '5', food_name: 'Salt'}
]
//我尝试过的代码:
var res = {};
var dep_data = [];
for (var j = 0; j < fruits.length; j++) {
for (var d = 0; d < food.length; d++) {
if (parseInt(fruits[j].id) != parseInt(food[d])) {
res["id"] = fruits[j].id;
res["name"] = fruits[j].name;
dep_data.push(res);
}
}
}
console.log(dep_data)
Please note that there's an issue in the code you provided. The condition in the inner loop should be parseInt(fruits[j].id) != parseInt(food[d].id)
to correctly compare the IDs. Additionally, you may want to reset the res
object inside the loop to avoid pushing the same object multiple times.
英文:
I want to compare these two arrays to get the id and name of the fruits that not exists in the food and save the results in new array.
For example here we have the Orange exist in the food array, the results should store the id and name of fruits that doesn't exist in food. (Apple and Cherry).
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const fruits = [{id: '1', name: 'Apple'},
{id: '2', name: 'Orange'},
{id: '3', name: 'Cherry'}];
const food=[{id: '1', creation_date: '2023-05-13 09:46:25', created_by: '1'},
{id: '1', food_name: 'Orange'},
{id: '2', food_name: 'Bread'},
{id: '3', food_name: 'Chees'},
{id: '4', food_name: 'Milk'},
{id: '5', food_name: 'Salt'}
]
//Code that I tried:
var res = {};
var dep_data = [];
for (var j = 0; j < fruits.length; j++) {
for (var d = 0; d < food.length; d++) {
if (parseInt(fruits[j].id) != parseInt(food[d])) {
res["id"] = fruits[j].id;
res["name"] = fruits[j].name;
dep_data.push(res);
}
}
}
console.log(dep_data)
<!-- end snippet -->
答案1
得分: 1
一个更简洁和高效的版本会首先使用map
将食物映射为一个名称数组,然后通过filter
创建一个新数组,该数组中排除了其name
值不在foodNames
数组中的水果对象。
const fruits = [{id: "1", name: "Apple"}, {id: "2", name: "Orange"}, {id: "3", name: "Cherry"}];
const food = [{id: "1", creation_date: "2023-05-13 09:46:25", created_by: "1"}, {id: "1", food_name: "Orange"}, {id: "2", food_name: "Bread"}, {id: "3", food_name: "Chees"}, {id: "4", food_name: "Milk"}, {id: "5", food_name: "Salt"}];
const foodNames = food.map(f => f.food_name);
const notInFood = fruits.filter(f => !foodNames.includes(f.name));
console.log(notInFood);
英文:
A more concise and efficient version would first map
over the foods to get an array of names, and then create a new array by filtering
out the fruits objects whose name
value is not included
in the foodNames
array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const fruits=[{id:"1",name:"Apple"},{id:"2",name:"Orange"},{id:"3",name:"Cherry"}];
const food=[{id:"1",creation_date:"2023-05-13 09:46:25",created_by:"1"},{id:"1",food_name:"Orange"},{id:"2",food_name:"Bread"},{id:"3",food_name:"Chees"},{id:"4",food_name:"Milk"},{id:"5",food_name:"Salt"}];
const foodNames = food.map(f => f.food_name);
const notInFood = fruits.filter(f => !foodNames.includes(f.name));
console.log(notInFood);
<!-- end snippet -->
答案2
得分: 0
你可以使用具有O(n + m)复杂度的Set,比嵌套循环O(n * m)更高效。
var foodSet = new Set(food.map(item => item.food_name));
for (var j = 0; j < fruits.length; j++) {
if (!foodSet.has(fruits[j].name)) {
dep_data.push({ id: fruits[j].id, name: fruits[j].name });
}
}
英文:
You can use Set with complexity O(n + m) more efficient than nested loop which is O(n * m)
var foodSet = new Set(food.map(item => item.food_name));
for(var j=0; j < fruits.length; j++){
if(!foodSet.has(fruits[j].name)){
dep_data.push({id: fruits[j].id, name: fruits[j].name});
}
}
答案3
得分: 0
以下是翻译好的代码部分:
const newArr = [];
for (let i = 0; i < fruits.length; i++) {
const findElement = food.find((obj) => obj.food_name ===
fruits[i].name);
if (!findElement) newArr.push(fruits[i]);
}
英文:
const newArr = [];
for (let i = 0; i < fruits.length; i++) {
const findElement = food.find((obj) => obj.food_name ===
fruits[i].name);
if (!findElement) newArr.push(fruits[i]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论