简体   繁体   中英

How to pass Array value inside Array of objects?

I'm new to Arrays and I want to pass and get value from my first array.

The code look like this:

My first array is:

const CompanyData = [
    {id: 1, name: 'Company1', phone: 'X', address: 'Y' , person: [1, 2]},
    {id: 2, name: 'Company2', phone: 'X', address: 'Y'},
    {id: 3, name: 'Company3', phone: 'X', address: 'Y'}
  ];

My second array is:

const PersonData = [
{id: 1, name: 'A', phone: 'D', address: 'X'},
{id: 2, name: 'B', phone: 'D', address: 'X'},
{id: 3, name: 'C', phone: 'D', address: 'X'}
];

How can I pass and get value from PersonData inside CompanyData on person array?

If this is possible, let me know. Thanks.

Are you looking something like this?

as per mentioned description.

 const CompanyData = [ {id: 1, name: 'Company1', phone: 'X', address: 'Y' ,person: [1,2]}, {id: 2, name: 'Company2', phone: 'X', address: 'Y'}, {id: 3, name: 'Company3', phone: 'X', address: 'Y'} ] const PersonData = [ {id: 1, name: 'A', phone: 'D', address: 'X'}, {id: 2, name: 'B', phone: 'D', address: 'X'}, {id: 3, name: 'C', phone: 'D', address: 'X'} ] CompanyData[0].person = [...PersonData]; console.log(CompanyData);

If what you want is to get a Person data as per the id then the better will be to create a function and from there return the element of the PersonData

const PersonData = [
    {id: 1, name: 'A', phone: 'D', address: 'X'},
    {id: 2, name: 'B', phone: 'D', address: 'X'},
    {id: 3, name: 'C', phone: 'D', address: 'X'}
]


const getPerson=(...args)=>{
    console.log(args)
    const Person=[];
    args.forEach(x=>{
        Person.push(PersonData.filter(y=>y.id===x))
    })
    return Person;
}
const CompanyData = [
{id: 1, name: 'Company1', phone: 'X', address: 'Y' , person: getPerson(1,2)},
{id: 2, name: 'Company2', phone: 'X', address: 'Y'},
{id: 3, name: 'Company3', phone: 'X', address: 'Y'}
]

You will get array inside CompanyData[0].person

You can edit the getPerson function as you want and it is just the idea of how you can make it compatible with multiple values and return anything.

Try this.

 const CompanyData = [ {id: 1, name: 'Company1', phone: 'X', address: 'Y' , person: [1,2]}, {id: 2, name: 'Company2', phone: 'X', address: 'Y'}, {id: 3, name: 'Company3', phone: 'X', address: 'Y'} ] const PersonData = [ {id: 1, name: 'A', phone: 'D', address: 'X'}, {id: 2, name: 'B', phone: 'D', address: 'X'}, {id: 3, name: 'C', phone: 'D', address: 'X'} ]; CompanyData.forEach(comp => { if (comp.person) { comp.person = comp.person.map(personId => PersonData.find(person => person.id === personId)); } }); console.log(CompanyData);

I wrote two script for that. in the first one I only show how to add person data in "companyData" array first object. but in the second one with a "for-in-loop" I added the data for each person to the each object of "companyData" array. I also changed the name of properties in "personData" array for clear understanding.

 //script1 let personData = [ {idPer: 1, namePer: 'Company1', phonePer: 'X', addressPer: 'Y'}, {idPer: 2, namePer: 'Company2', phonePer: 'X', addressPer: 'Y'}, {idPer: 3, namePer: 'Company3', phonePer: 'X', addressPer: 'Y'} ]; let person = personData[1].namePer; let companyData = [ {id: 1, name: 'Company1', phone: 'X', address: 'Y' , personDataInp: person}, {id: 2, name: 'Company2', phone: 'X', address: 'Y'}, {id: 3, name: 'Company3', phone: 'X', address: 'Y'} ]; console.log(companyData); //script2 let personData2 = [ {idPer: 1, namePer: 'Company1', phonePer: 'X', addressPer: 'Y'}, {idPer: 2, namePer: 'Company2', phonePer: 'X', addressPer: 'Y'}, {idPer: 3, namePer: 'Company3', phonePer: 'X', addressPer: 'Y'} ]; let companyData2 = [ {id: 1, name: 'Company1', phone: 'X', address: 'Y'}, {id: 2, name: 'Company2', phone: 'X', address: 'Y'}, {id: 3, name: 'Company3', phone: 'X', address: 'Y'} ]; for(i in companyData2) { companyData2[i].personDataInp = personData2[i].namePer; } console.log(companyData2);

How about Map instead?

The way I see it, arrays aren't a good data structure option for what you're trying to achieve. Since it has no keys, you'd have to iterate through all of the arrays until you're finding what you're looking for. Which is fine for this amount of data but can get really slow when things start to scale.

You could use Map instead. Which uses a key to identify value.

// Crete a Map Object
const PersonMap = new Map();

// Convert your Array to Map Format
PersonData.forEach(function(person) {
  PersonMap.set(person.id, person);
});

// Get a person by Id
console.log(PersonMap.get(1)); // Outputs {id: 1, name: 'A', phone: 'D', address: 'X'}

So with that, in CompanyData[0].person (or any other, of course) you can then push PersonMap.get(1) and PersonMap.get(2).

// Make it an empty array
CompanyData[0].person = [];

// Push a single entry for each person
CompanyData[0].person.push(PersonMap.get(1));
CompanyData[0].person.push(PersonMap.get(2));

// This will give the intended result

Or you could batch push it all.

// Push all, automatically
CompanyData.forEach(function(company) {
  if(company.person) { // Since some companies doesn't have any person
    company.person = company.person.map(function(person) {
      return PersonMap.get(person);
    });
  }
});

What if I wanted to use Maps since the beginning?

You'll have to declare the key. Map.size is like the length property of the array, returning the amount os entries inside. So you could build the Map like this:

const PersonData = new Map();

PersonData.set(PersonData.size + 1, {
  id: PersonData.size + 1,
  name: 'A',
  phone: 'D',
  address: 'X'
});

More of Maps, here .


Nope? Only arrays? Ok

Whatever, if you want to still use an array, I'll have to check if the id is equal though every iteration, until you find what you're looking for, resulting in something like this:

// For + If Solution
CompanyData.forEach(function(company) { // For every company
  if(company.person) { // If company has persons
    company.person = company.person.map(function(member) { // For every person in company (member)
      for(let person of PersonData) { // Looks for every person
        if(member === person.id) return person; // If is the person
      }
    });
  }
});
// Filter Solution
CompanyData.forEach(function(company) { // For every company
  if(company.person) { // If company has persons
    company.person = company.person.map(function(member) { // For every person in company (member)
      return PersonData.filter(function(person) { return person.id === member });
    });
  }
});

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