过滤对象以在另一个数组中获取值

时间:2017-02-01 21:55:55

标签: javascript arrays object

我有一个对象和一个数组

 {
    "library_items": 
    [

    {
        "id": "23493",
        "artifactID": "",
        "title":"Physics Lecture",
        "authors": [{
                    "name": "Don Johnson",
                    "artifactID": "",
                    "role": "author",
                    "roleID": ""
                    }],
        "artifactType": "games",
        "domain":   {
                    "branch": "",
                    "description": "",
                    "branchInfo": {
                                    "branch": "",
                                    "subject": "Physics"
                                }
                    },
        "type": {
                "description": "",
                "id": "",
                "name": ""
        }
    },


   {
        "id": "23493",
        "artifactID": "",
        "title":"Chemistry Lecture",
        "authors": [{
                    "name": "Don Johnson",
                    "artifactID": "",
                    "role": "author",
                    "roleID": ""
                    }],
        "artifactType": "games",
        "domain":   {
                    "branch": "",
                    "description": "",
                    "branchInfo": {
                                    "branch": "",
                                    "subject": "Chemistry"
                                }
                    },
        "type": {
                "description": "",
                "id": "",
                "name": ""
        }
    }
    ]
  }

我有另一个数组

  var subjects = ['Physics', 'Biology', 'Mathematics']

如何使用" subject"?中的数组值过滤Object。我的意思是我想从对象中获取具有与任何数组值对应的主题的项目。

5 个答案:

答案 0 :(得分:2)

基本上,使用indexOffilter方法。

  1. indexOf 根据回调函数中定义的匹配条件,使用此方法过滤数组。
  2. includes 检查数组是否包含值 注意:还有另一种方法var items = { "library_items": [ { "id": "23453", "artifactID": "", "title":"Physics Basics", "subject": "Physics" }, { "id": "23453", "artifactID": "", "title":"Chemistry Basics", "subject": "Chemistry" } ] }; var subjects = ['Physics', 'Biology', 'Mathematics']; var filtered = items.library_items.filter((x) => subjects.indexOf(x.subject) > -1); console.log(filtered);可以检查数组中是否存在元素,这是ES7草案的一部分,但很多浏览器都不支持。
  3. namespace Company\Module\Model;
    
    class Vendor extends \Magento\Framework\Model\AbstractModel {
        protected function _constructor() {
            $this->_init('Company\Module\Model\Resource\Vendor');
        }
    }
    

答案 1 :(得分:0)

您可以使用thisArray#filter



var items = { library_items: [{ id: "23453", artifactID: "", title: "Physics Basics", subject: "Physics" }, { id: "23453", artifactID: "", title:"Chemistry Basics", subject: "Chemistry" }] },
    subjects = ['Physics', 'Biology', 'Mathematics'],
    filtered = items.library_items.filter(x => subjects.includes(x.subject));

console.log(filtered);




答案 2 :(得分:0)

我在每个项目中使用lodash(https://lodash.com/docs)。他们构建了大量的方法和包装器,使您的生活更轻松。

┌─[shirish@debian] - [~/games/libcpuid] - [10328]
└─[$] git-info

## Remote URLs:

origin  https://github.com/anrieff/libcpuid (fetch)
origin  https://github.com/anrieff/libcpuid (push)

## Remote Branches:

  origin/HEAD -> origin/master
  origin/master

## Local Branches:

* master

## Most Recent Commit:

commit fa87a5e183a3809ded89ecb3efde4e94837c5736
Author: Veselin Georgiev <anrieff@gmail.com>

    Fixed issue #78: date in changelog is wrong

Type 'git log' for more commits, or 'git show <commit id>' for full commit details.

## Configuration (.git/config):

user.name=Shirish Agarwal
user.email=shirishag75@gmail.com
core.editor=leafpad
core.excludesfiles=/home/shirish/.gitignore
core.gitproxy="ssh" for gitorious.org
merge.tool=meld
push.default=simple
color.ui=true
color.status=auto
color.branch=auto
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/anrieff/libcpuid
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

如果您不想安装其他套餐,我会选择Agalo的答案!

答案 3 :(得分:0)

如果表现对您很重要。我建议你采取另一种方法。

使用filter和indexOf(或包含),您的算法性能为O(n * m)。对于每个图书馆项目,您需要浏览每个科目。

如果首先从subject数组创建一个简单的哈希表,则可以归档O(n + m)。首先,您浏览每个主题并将其写入哈希表,然后对于每个库项目,您可以使用O(1)(因为哈希表)检查是否需要该项目。

以下是代码示例:

&#13;
&#13;
var items = {
 "library_items": 
    [
    {
        "id": "23453",
        "artifactID": "",
        "title":"Physics Basics",
        "subject": "Physics"

    },
    {
        "id": "23453",
        "artifactID": "",
        "title":"Chemistry Basics",
        "subject": "Chemistry"

    }

    ]
};
var subjects = ['Physics', 'Biology', 'Mathematics'];
var allSubjects = {};
subjects.forEach(s => allSubjects[s] = true);
var filtered = items.library_items.filter(x => allSubjects[x.subject]);
console.log(filtered);
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

 result = obj.library_items.filter(o => subjects.indexOf(o.subject) > -1)