JavaScript查找包含另一个对象中数组匹配元素的对象。

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

Javascript find objects that contain matching elements from arrays in another object

问题

Here's the translated code:

我有两个对象

const people = [
{
    name: "Dave",
    fruit: ["apple", "pear"],
    veg: ["asparagus", "peas"]
},
{
    name: "Frank",
    fruit: ["mango", "banana"],
    veg: ["sweetcorn"]
},
{
    name: "Alice",
    fruit: ["mango", "peach"],
    veg: ["asparagus"]
}];

const demographics = {
  fruit: ["apple", "mango"],
  veg: ["asparagus"]
}

我想要找出哪些'people'喜欢水果'apple''mango'以及蔬菜'asparagus'这些条件由'demographics'对象定义如下所示

[{
    name: "Dave",
    fruit: ["apple", "pear"],
    veg: ["asparagus", "peas"]
},
{
    name: "Alice",
    fruit: ["mango", "peach"],
    veg: ["asparagus"]
}]

希望这可以帮助你解决问题。如果有可能,我建议使用underscore,但这不是必需的。

英文:

I have two objects:

const people = [
{
    name: "Dave",
    fruit: ["apple", "pear"],
    veg: ["asparagus", "peas"]
},
{
    name: "Frank",
    fruit: ["mango", "banana"],
    veg: ["sweetcorn"]
},
{
    name: "Alice",
    fruit: ["mango", "peach"],
    veg: ["asparagus"]
}];

const demographics = {
  fruit: ["apple", "mango"],
  veg: ["asparagus"]
}

I would like to find which 'people' like the fruit 'apple' or 'mango' and the veg 'asparagus', as defined in the 'demographics' object, like so:

[{
    name: "Dave",
    fruit: ["apple", "pear"],
    veg: ["asparagus", "peas"]
},
{
    name: "Alice",
    fruit: ["mango", "peach"],
    veg: ["asparagus"]
}]

I've been staring at this all day and am not advanced enough to dig into objects like this. Can anyone help? If possible, I would like to use underscore, but not essential.

答案1

得分: 1

我已经翻译了您提供的代码注释部分:

// 首先,我将人口统计对象转换为数组,以便轻松迭代。之后,使用条件筛选人员数组,检查每个人口统计中至少有一个满足条件(位于每个人的蔬菜或水果数组中)。
// 如果您想使用underscore来转换上面的代码,您可以找到等效的方法 `pairs`,`filter`,`some`,`every`,`contains`。

const people = [
    {
        name: "Dave",
        fruit: ["apple", "pear"],
        veg: ["asparagus", "peas"]
    },
    {
        name: "Frank",
        fruit: ["mango", "banana"],
        veg: ["sweetcorn"]
    },
    {
        name: "Alice",
        fruit: ["mango", "peach"],
        veg: ["asparagus"]
    }];

const demographics = {
  fruit: ["apple", "mango"],
  veg: ["asparagus"]
}

const demographicsEntries = Object.entries(demographics)

const res = people.filter(person => demographicsEntries.every(([k,v]) => v.some(val => person[k].includes(val))))

console.log(res)

请注意,这段代码主要用于筛选符合指定条件的人员,以满足每个人口统计中至少有一个满足的条件。如果您有任何进一步的问题或需要其他帮助,请随时告诉我。

英文:

first I converted the demographics object to an array so i can iterate easily. after that filter the person array with the conditions
check every demographic where in each demographic at least 1 satisfies (is inside the veg or fruit.. arrays of a person).
You can find equivalent methods pairs,filter,some,every,contains if you want to convert above using underscore

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const people = [
{
    name: &quot;Dave&quot;,
    fruit: [&quot;apple&quot;, &quot;pear&quot;],
    veg: [&quot;asparagus&quot;, &quot;peas&quot;]
},
{
    name: &quot;Frank&quot;,
    fruit: [&quot;mango&quot;, &quot;banana&quot;],
    veg: [&quot;sweetcorn&quot;]
},
{
    name: &quot;Alice&quot;,
    fruit: [&quot;mango&quot;, &quot;peach&quot;],
    veg: [&quot;asparagus&quot;]
}];

const demographics = {
  fruit: [&quot;apple&quot;, &quot;mango&quot;],
  veg: [&quot;asparagus&quot;]
}

const demographicsEntries = Object.entries(demographics)

const res = people.filter(person =&gt; demographicsEntries.every(([k,v]) =&gt; v.some(val =&gt; person[k].includes(val))))

console.log(res)

<!-- end snippet -->

答案2

得分: 0

这里的人是一个对象数组。

const people = [
  {
    name: "Dave",
    fruit: ["apple", "pear"],
    veg: ["asparagus", "peas"]
  },
  {
    name: "Frank",
    fruit: ["mango", "banana"],
    veg: ["sweetcorn"]
  },
  {
    name: "Alice",
    fruit: ["mango", "peach"],
    veg: ["asparagus"]
  }
];

const demographics = {
  fruit: ["apple", "mango"],
  veg: ["asparagus"]
};

people.filter(person =>
  person.fruit.some(fruitName => demographics.fruit.includes(fruitName)) &&
  person.veg.some(vegName => demographics.veg.includes(vegName)) &&
  person
);

希望这有所帮助 (y)

英文:

people in here is an array of objects.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const people = [
{
    name: &quot;Dave&quot;,
    fruit: [&quot;apple&quot;, &quot;pear&quot;],
    veg: [&quot;asparagus&quot;, &quot;peas&quot;]
},
{
    name: &quot;Frank&quot;,
    fruit: [&quot;mango&quot;, &quot;banana&quot;],
    veg: [&quot;sweetcorn&quot;]
},
{
    name: &quot;Alice&quot;,
    fruit: [&quot;mango&quot;, &quot;peach&quot;],
    veg: [&quot;asparagus&quot;]
}];

const demographics = {
  fruit: [&quot;apple&quot;, &quot;mango&quot;],
  veg: [&quot;asparagus&quot;]
};

people.filter(person =&gt;
    person.fruit.some(fruitName=&gt; demographics.fruit.includes(fruitName)) &amp;&amp; person.veg.some(vegName=&gt; demographics.veg.includes(vegName)) &amp;&amp; person);

<!-- end snippet -->

Hope this will help (y)

答案3

得分: 0

现代JavaScript提供了许多非常有用的数组和对象方法,您可以使用它们来减少Underscore的额外代码。以下是您提供的代码部分:

const data = [
  { name: 'Dave', fruit: ['apple', 'pear'], veg: ['asparagus', 'peas'] },
  { name: 'Frank', fruit: ['mango', 'banana'], veg: ['sweetcorn'] },
  { name: 'Alice', fruit: ['mango', 'peach'], veg: ['asparagus'] }
];

function find(data, query) {
  return data.filter(person => {
    return Object.keys(query).every(key => {
      return query[key].some(el => {
        return person[key].includes(el);
      });
    });
  });
}

const query = { fruit: ['apple', 'mango'], veg: ['asparagus'] };
console.log(find(data, query));

const query2 = { fruit: ['apple'], veg: ['peas'] };
console.log(find(data, query2));

const query3 = { fruit: ['banana'], veg: ['peas'] };
console.log(find(data, query3));

您可以查看附加文档以了解有关filtereverysomeincludes等方法的更多信息。

英文:

Modern JavaScript has a lot of really useful array and object methods that you can use to spare the additional code footprint of Underscore.

In essence (links to documentation below) you want to filter out all of the people who have at least some of the elements in their fruit/veg arrays that appear in every corresponding query array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data=[{name:&#39;Dave&#39;,fruit:[&#39;apple&#39;,&#39;pear&#39;],veg:[&#39;asparagus&#39;,&#39;peas&#39;]},{name:&#39;Frank&#39;,fruit:[&#39;mango&#39;,&#39;banana&#39;],veg:[&#39;sweetcorn&#39;]},{name:&#39;Alice&#39;,fruit:[&#39;mango&#39;,&#39;peach&#39;],veg:[&#39;asparagus&#39;]}];

// Accepts some data, and the query object
function find(data, query) {
  
  // `filter` out all the people...
  return data.filter(person =&gt; {
    
    // ...who, for every query key (fruit/vegetable),
    // have at least one item in their fruit/veg array
    // that includes an item in the corresponding query array
    return Object.keys(query).every(key =&gt; {
      return query[key].some(el =&gt; {
        return person[key].includes(el);
      });
    });
  });
}

const query={fruit:[&#39;apple&#39;,&#39;mango&#39;],veg:[&#39;asparagus&#39;]};
console.log(find(data, query));

const query2={fruit:[&#39;apple&#39;],veg:[&#39;peas&#39;]};
console.log(find(data, query2));

const query3={fruit:[&#39;banana&#39;],veg:[&#39;peas&#39;]};
console.log(find(data, query3));

<!-- end snippet -->

Additional documentation

huangapple
  • 本文由 发表于 2023年5月11日 00:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220897.html
匿名

发表评论

匿名网友

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

确定