与参数匹配时返回对象的键和值 [英] Return keys and values of object when matching with argument

查看:50
本文介绍了与参数匹配时返回对象的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是构建一个函数,该函数将Objects数组作为第一个参数.作为第二个参数,它采用键和值对.

Aim is to build a function, which takes an array of Objects as first arguments. As a second argument it takes a key and value pair.

function whatIsInAName(collection, source)

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

Now the function should return an array with all the matching keys and values from the object. For example:

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

应该返回

[{第一个:"Tybalt",最后一个:"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 }]

我尝试遍历整个集合,并遍历源数组以查找匹配项.像这样:

I tried looping through the collection- and through the source array searching for matches. Like this:

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

这并不奇怪.

我不明白的是,为什么我不能归纳以上解决方案,以便在其他情况下也能使用.

What I don't understand is, why I can't generalize the above solution, so that it also works in other cases.

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

if (collection[i] === source)

(请注意集合和源后面缺少" Last" 键.)

(Pls notice the missing "Last" key behind collection and source.)

当我 console.log(source)时,控制台将其记录下来.因此,恕我直言,上面的条件语句应该起作用,并且应该将匹配项推入 arr 数组.

When I console.log(source), console logs it. So imho the conditional statement above should work and should go to push the matches into the arr Array.

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

 function whatIsInAName(collection, source)

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

谢谢.

推荐答案

首先:

为什么不能简单地给出条件: if(collection [i] === source)

因为对象是通过引用值进行比较的,所以它们是两个不同的对象,因此它们的引用是不同的.

Because objects are compared by reference values, they are two different objects, so they references are distinct.

现在,您可以使用以下方法修正逻辑:

Now, you could fix you logic with something like this:

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

这篇关于与参数匹配时返回对象的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆