比较两个对象中相等值的方法。

huangapple go评论61阅读模式
英文:

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: &#39;1&#39;, name: &#39;Apple&#39;},
{id: &#39;2&#39;, name: &#39;Orange&#39;},
{id: &#39;3&#39;, name: &#39;Cherry&#39;}];

const food=[{id: &#39;1&#39;, creation_date: &#39;2023-05-13 09:46:25&#39;, created_by: &#39;1&#39;},
{id: &#39;1&#39;, food_name: &#39;Orange&#39;},
{id: &#39;2&#39;, food_name: &#39;Bread&#39;},
{id: &#39;3&#39;, food_name: &#39;Chees&#39;},
{id: &#39;4&#39;, food_name: &#39;Milk&#39;},
{id: &#39;5&#39;, food_name: &#39;Salt&#39;}
]
//Code that I tried:
var res = {};
var dep_data = [];
for (var j = 0; j &lt; fruits.length; j++) {
  for (var d = 0; d &lt; food.length; d++) {
    if (parseInt(fruits[j].id) != parseInt(food[d])) {
      res[&quot;id&quot;] = fruits[j].id;
      res[&quot;name&quot;] = 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:&quot;1&quot;,name:&quot;Apple&quot;},{id:&quot;2&quot;,name:&quot;Orange&quot;},{id:&quot;3&quot;,name:&quot;Cherry&quot;}];
const food=[{id:&quot;1&quot;,creation_date:&quot;2023-05-13 09:46:25&quot;,created_by:&quot;1&quot;},{id:&quot;1&quot;,food_name:&quot;Orange&quot;},{id:&quot;2&quot;,food_name:&quot;Bread&quot;},{id:&quot;3&quot;,food_name:&quot;Chees&quot;},{id:&quot;4&quot;,food_name:&quot;Milk&quot;},{id:&quot;5&quot;,food_name:&quot;Salt&quot;}];

const foodNames = food.map(f =&gt; f.food_name);
const notInFood = fruits.filter(f =&gt; !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 =&gt; item.food_name));
for(var j=0; j &lt; 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 &lt; fruits.length; i++) {
  const findElement = food.find((obj) =&gt; obj.food_name === 
    fruits[i].name);
   if (!findElement) newArr.push(fruits[i]);
}

huangapple
  • 本文由 发表于 2023年5月13日 22:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定