在此示例中,如何比较数组值和对象属性?

时间:2018-09-23 08:15:44

标签: javascript jquery reactjs lodash

这是我的数组:

const ids = [
  "id1",
  "id2",
  "id3"
]

通过cookie提取的对象

const data = [
    {
      id: "id3" // dynamically populated and can vary, we need this property
      prop2: "prop2", // not needed
      prop3: "prop3" // not needed
    }
]

如何比较第一个数组值-idsdata.id,如果true,则显示一些逻辑(例如-console.log(true)?我在{{ 1}}。

注意if statement。可以是data.idid1之间的任何内容。我需要找到一种动态比较这些值的方法。在“现实世界”中,它id3所做的任何事情都1,但这只是该示例的模型。

我还要感谢Lodash的例子。

4 个答案:

答案 0 :(得分:1)

您可以迭代Array中所有作为对象的元素,并使用Object.keys,迭代该对象中可以与初始值数组进行比较的所有键。

const ids = [
  "id1",
  "id2",
  "id3"
];

const data = [{
  id: "id3", // dynamically populated and can vary, we need this property
  prop2: "prop2", // not needed
  prop3: "prop3" // not needed
}];

const foundElems = [];
data.forEach((el) => {
  Object.keys(el).forEach(elem => {
    if (ids.indexOf(el[elem]) > -1) {
      var Obj = {};
      Obj[elem] = el[elem];
      foundElems.push(Obj);
    }
  });
});
console.log(foundElems);

答案 1 :(得分:0)

我认为这就是您想要的:

if (ids.indexOf(data.id) !== -1){
  console.log('true')
}

答案 2 :(得分:0)

对于数据中的每个项目,请检查ID是否在ID数组中,如果是,则执行某些操作

const ids = [
    "id1",
    "id2",
    "id3"
]

const data = [
  {
      id: "id3", // dynamically populated and can vary, we need this property
      prop2: "prop2", // not needed
      prop3: "prop3" // not needed
   }
]

// if we have the id in ids array, do something
if (data.some(d => ids.indexOf(d.id) > -1)) {
  // do something
}

使用lodash:

const ids = [
    "id1",
    "id2",
    "id3"
]

const data = [
  {
      id: "id3", // dynamically populated and can vary, we need this property
      prop2: "prop2", // not needed
      prop3: "prop3" // not needed
   }
]

// if we have the id in ids array, do something
if (_.some(data, d => ids.indexOf(d.id) > -1)) {
  // do something
}

答案 3 :(得分:0)

只需运行下一个代码:

<html>
<body>
    <button onclick="compareIds()">Click me</button>
    <script>
        const ids = [
              "id1",
              "id2",
              "id3"
            ]

        const data = [
                {
                  id: "id3", // dynamically populated and can vary, we need this property
                  prop2: "prop2", // not needed
                  prop3: "prop3" // not needed
                }
            ]

        function compareIds(){
                var dataObject = findDataInObject();
                for(var i = 0; i < ids.length; i++){
                        if (ids[i] == dataObject){
                            console.log("true");
                        }

                        else{
                            if(i == ids.length -1){
                                console.log("false");
                            }
                    }
                }
        }

        function findDataInObject(){
                    for(key in data) {
                        if(data.hasOwnProperty(key)) {
                            var ArrayOfData = data[key];
                            var IdOfData = ArrayOfData.id;
                            return IdOfData;
                        }
                    }
                }


    </script>
</body>