Return 与参数匹配时 object 的键和值

Return keys and values of object when matching with argument

目的是构建一个函数,该函数将 Objects 的数组作为第一个参数。作为第二个参数,它需要一个键值对。

function whatIsInAName(collection, source)

现在函数应该 return 一个包含 object 中所有匹配键和值的数组。例如:

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })´ 

应该return

[{ first: "Tybalt", last: "Capulet" }]

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })

应该return

 [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]

我尝试遍历 collection- 并通过源数组搜索匹配项。像这样:

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  for (var i = 0; i < Object.keys(collection).length; i++) {
    for (var j = 0; j < Object.keys(source).length; j++) {
      if (collection[i].last === source.last) {
        arr.push(collection[i]);
      }
    }
  }
  console.log(arr);
}

在这个参数的情况下 whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })

这并不奇怪。

我不明白的是,为什么我不能概括上述解决方案,以便它在其他情况下也适用。

为什么不能简单的给出条件:

if (collection[i] === source)

(请注意 collection 和来源后面缺少的 "Last" 键。)

当我 console.log(source) 时,控制台会记录它。所以恕我直言,上面的条件语句应该有效,并且应该将匹配项推送到 arr 数组中。

如何构建一个函数,其中 return 是一个数组,其中包含来自 object.

的所有匹配键和值
 function whatIsInAName(collection, source)

为什么 (collection[i] === source) 不起作用?

谢谢。

您可以使用过滤器和每个。按对象。 所以这里的想法是

  1. input arg 上使用过滤器以在输出中获取所需的元素。
  2. Object.keys 的过滤器回调中,我们从 matchObj arg 中取出键,对于每个键,我们将元素的值与我们在 matchObj arg 中获得的值进行匹配,并进行相应的过滤。

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }))

function whatIsInAName(input,matchObj){
  return input.filter(e=> Object.keys(matchObj).every(el=> e[el] === matchObj[el]))
}

您需要迭代 key/value 对和 return 过滤结果。

实际上没有内置方法来测试一个对象与另一个对象的对比,以检查该对象是否包含子对象。

function whatIsInAName(collection, source) {
    var pairs = Object.entries(source);
    return collection.filter(object => pairs.every(([key, value]) => object[key] === value));
}

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

第一个:

Why is it not possible to simply give the condition: if (collection[i] === source)

因为objects是通过引用值比较的,是两个不同的对象,所以引用是不同的。

现在,你可以用这样的东西来修复你的逻辑:

function whatIsInAName(collection, source)
{
  // What's in a name?

  var arr = [], sourceKeys = Object.keys(source);
  var toPush, key;

  // Iterate over the collection of input objects.

  for (var i = 0; i < collection.length; i++)
  {
    toPush = true;

    // Check if current analyzed object have all
    // required (key, value) pairs.

    for (var j = 0; j < sourceKeys.length; j++)
    {
      key = sourceKeys[j];

      if (!collection[i][key] || collection[i][key] !== source[key])
      {
        toPush = false;
        break;
      }
    }

    // If current object meet the condition, put it on the
    // result array.

    if (toPush)
      arr.push(collection[i]);
  }

  return arr;
}

let input1 = [
  {first: "Romeo", last: "Montague"},
  {first: "Mercutio", last: null},
  {first: "Tybalt", last: "Capulet"}
];

console.log(whatIsInAName(input1, {last: "Capulet"}));

let input2 = [
  {"apple": 1, "bat": 2},
  {"bat": 2},
  {"apple": 1, "bat": 2, "cookie": 2}
];

console.log(whatIsInAName(input2, {"apple": 1, "bat": 2}));