简体   繁体   中英

How to check if an id in one array of json objects exists in another nested array of json objects?

I am making a call to two end points and need to display all the companies with their funds,name and factory that produces for that company.

here is the response from one end point

let factories = [
    {
        id: 1,
        name: "Xintang",
        short: "xin",
        companies: [0, 4, 101,198]
    },
    {
        id: 2,
        name: "Ohio Plant",
        short: "OHP",
        companies: [22, 27]
    },
    {
        id: 3,
        name: "Cincy",
        short: "Cin",
        companies: []
    }
];

Here is the response from the second

let companies = [
    {
        id: 0,
        fund: "79588.96",
        name: "Microsoft"
    },
    {
        id: 1,
        fund: "166727.06",
        name: "Comcast"
    },
    {
        id: 2,
        fund: "131206.88",
        name: "Apple"
    },
    {
        id: 3,
        fund: "74095.75",
        name: "HP"
    },
    {
        id: 4,
        fund: "142556.86",
        name: "Dell"
    }
];

the dataset is much bigger, but here is just a sample. So I want be able to create a new object that links the factory with the specific company. Is there a way I can map over the companies and check which factory has the company id in that nested array so that I can add a new property factory to the company, and have a new array of objects that would look like this.

let returnedArr = [
    {
        id: 0,
        fund: "79588.96",
        name: "Microsoft",
        factory: "Xintang"
    },
    {
        id: 4,
        fund: "142556.86",
        name: "Dell",
        factory: "Xintang"
    }
];

Try This.... It may help u...

    let result = [];
    companies.forEach(company => {
        let tempCompany = {...company};
        factories.forEach(factory => {
            let tempArray = factory.companies.filter(item => item === company.id);
            if(tempArray.length > 0) {
                tempCompany.factory = factory.name;
            }
        });

        result.push(tempCompany);
    });

You can do the following using reduce and Map .

Get the company-id and factory-name Map -> Then loop through the companies and create the output

 let factories = [{id:1,name:"Xintang",short:"xin",companies:[0,4,101,198]},{id:2,name:"Ohio Plant",short:"OHP",companies:[22,27]},{id:3,name:"Cincy",short:"Cin",companies:[]}], companies = [{id:0,fund:"79588.96",name:"Microsoft"},{id:1,fund:"166727.06",name:"Comcast"},{id:2,fund:"131206.88",name:"Apple"},{id:3,fund:"74095.75",name:"HP"},{id:4,fund:"142556.86",name:"Dell"}] /*Get the company id: factory name mapping*/ const map = factories.reduce((m, f) => (f.companies.forEach(c => m.set(c, f.name)), m) , new Map); const output = companies.map(c => ({...c, factory: map.get(c.id) || ''})); console.log(output) 

One way to do this is to create a map of company ids to factory ids, then just iterate through your companies array and add the corresponding factory to the company object, like so:

The big advantage of this is that your factoryid lookups will be O(1) , and it is O(n) to build the map. Your entire algorithm will be O(n) . This makes this extremely fast even for very large data sets.

 let factories = [ { id: 1, name: "Xintang", short: "xin", companies: [0, 4, 101,198] }, { id: 2, name: "Ohio Plant", short: "OHP", companies: [22, 27] }, { id: 3, name: "Cincy", short: "Cin", companies: [] } ]; let companies = [ { id: 0, fund: "79588.96", name: "Microsoft" }, { id: 1, fund: "166727.06", name: "Comcast" }, { id: 2, fund: "131206.88", name: "Apple" }, { id: 3, fund: "74095.75", name: "HP" }, { id: 4, fund: "142556.86", name: "Dell" } ]; var factoryMap = factories.reduce((res, curr) => { return Object.assign(res, curr.companies.reduce((_res, _curr) => (_res[_curr] = curr.name, res), {})) }, {}); var mappedCompanies = companies.map(company => Object.assign(company, {factory: factoryMap[company.id] || ""})); console.log(mappedCompanies); 

Assuming that a company can have more than one factory.

Try the following

 let factories = [{ id: 1, name: "Xintang", short: "xin", companies: [0, 4, 101, 198] }, { id: 2, name: "Ohio Plant", short: "OHP", companies: [22, 27] }, { id: 3, name: "Cincy", short: "Cin", companies: [] } ]; let companies = [{ id: 0, fund: "79588.96", name: "Microsoft" }, { id: 1, fund: "166727.06", name: "Comcast" }, { id: 2, fund: "131206.88", name: "Apple" }, { id: 3, fund: "74095.75", name: "HP" }, { id: 4, fund: "142556.86", name: "Dell" } ]; let returnedArr = companies.map(company => { company.factories = factories .filter(factory => factory.companies.includes(company.id)) .map(factory => factory.name); return company; }); console.log(JSON.stringify(returnedArr, null, 4)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM