【问题标题】:Return keys and values of object when matching with argument [duplicate]与参数匹配时返回对象的键和值[重复]
【发布时间】:2019-06-30 17:01:14
【问题描述】:

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

function whatIsInAName(collection, source)

现在函数应该返回一个数组,其中包含对象中所有匹配的键和值。例如:

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

应该返回

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

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

应该返回

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

我尝试循环遍历集合并通过源数组搜索匹配项。像这样:

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)

(请注意集合和来源后面缺少的“Last"”键。)

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

如何构建一个函数,该函数返回一个包含对象中所有匹配键和值的数组。

 function whatIsInAName(collection, source)

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

谢谢。

【问题讨论】:

  • 如果两个对象是对一个对象的相同引用,则它们只是===

标签: javascript arrays object matching


【解决方案1】:

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

  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]))
}

【讨论】:

    【解决方案2】:

    您需要迭代键/值对并返回过滤结果。

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

    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; }

    【讨论】:

      【解决方案3】:

      第一:

      为什么不能简单地给出条件: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}));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 2014-01-08
        • 2020-10-07
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多