left join two object arrays

davilu
Mega Sage

I have a question regarding joining two object arrays. I have one object array that contains (for example) ten animals:

data.list = []; 
data
.list.push({
        animal
: wf.getDisplayValue('name')
});

and I have another object array that contains the attributes of the only two of the animals:

data.attributes = []; 
data
.attributes.push({
        animal_height
: wf.getDisplayValue('height'),
        animal_weight
: wf.getDisplayValue('weight'),
        animal_family
: wf.getDisplayValue('family'),
        animal_age
: wf.getDisplayValue('age')
});

Is there a way for me to left join these two arrays so that ALL ten animals are displayed in a list, along with the attributes associated with the two animals with additional details?

3 REPLIES 3

bernyalvarado
Mega Sage

Hi David,



Are you forced to use that data structure? How do you know which attributes are related to which animal?



Still, leaving that aside, take a look to some of the responses given on this thread: javascript - Merge 2 arrays of objects - Stack Overflow . I believe some of these will be helpful for what you're trying to accomplish.



I hope it helps!



Thanks,


Berny


Hi bernyalvarado, sorry for the confusion, the second array would have the animal name as well:



data.attributes = [];
data
.attributes.push({


        animal: wf.getDisplayValue('name'),
        animal_height
: wf.getDisplayValue('height'),
        animal_weight
: wf.getDisplayValue('weight'),
        animal_family
: wf.getDisplayValue('family'),
        animal_age
: wf.getDisplayValue('age')
});



Also, I'm not forced into this data structure, do you have a cleaner, more efficient way of doing something like this?



Hi David,



If the second array contains the name of the animal, why would you need the first array that only contains the name of the animal?



Thanks,


Berny