I know there have been some similar questions before but I'm really stuck on trying to map the below array of information (I've tried to implement several example). I have an Array with two information fields and a third field that contains Arrays of objects. I want to extract the name of each of the objects into the original name so that my output looks like the below:
Desired Output:
[gameId, gameName, gameGenresArray]
Below is a sample of what the data looks like:
Data = [ 270722, 'The Wild at Heart', [ [Object], [Object], [Object] ] ],
[ 558984, 'Knockout City', [ [Object] ] ],
[ 558982, 'Miitopia', [ [Object], [Object] ] ],
[ 45775, 'Biomutant', [ [Object], [Object] ] ]
The [Object] has a property called gameGenre that I want to store in the original array as an array instead of as an Array of Objects.
My most recent attempt was:
var result = data.map(({ id, name, [{gameGenres}] }) => ([id, name, gameGenres]))
I appreciate any help anyone can add!
Thanks!!
I think this is what you want:
const Data = [
[
270722,
'The Wild at Heart', [{
name: 'action'
}, {
name: 'horror'
}, {
name: 'adventure'
}],
],
[558984, 'Knockout City', [{
name: 'action'
}]],
[558982, 'Miitopia', [{
name: 'action'
}, {
name: 'rpg'
}]],
[45775, 'Biomutant', [{
name: 'casual'
}, {
name: 'platform'
}]],
];
const result = Data.map(item => {
return {
gameId: item[0],
gameName: item[1],
gameGenresArray: item[2].map(genre => genre.name),
};
});
console.log(result);
You need to map each object and then - when you have an array in an object - map within the map. Easiest to save them in variables:
let gameid, gamename, gamegenre;
Data.map((game) => {gameid=game.id;
gamename=game.name;
game.map((genre) => gamegenre+=genre)})
let result = [gameid, gamename, gamegenre]
Probably needs to be modified, but I don't know how you objects looks.
Related
I have an array like this.
let Array = [{
id: 1,
name: "Car",
cars: [{
id: 2,
name: "Hyundai",
models: [
"Verna", "Aura", "Azera", "Accent", "Sonata"
]
}]
}....]
And I think this not the best way: Array.map(car => car.cars.map(model => model.models)).
Is there any other way?
There is no 'short way' as what you are doing is very specific to your problem. The way you wrote it is the most concise if you want to end up with an array like:
let Array = [
[
[ "Verna", "Aura", "Azera", "Accent", "Sonata" ],
[ ... ],
],
[ ... ],
]
But if you want to flatten the array, you can consider writing it as:
Array
.map(car => car.cars).flat()
.map(model => model.models).flat()
This will result in an array like this:
[ "Verna", "Aura", "Azera", "Accent", "Sonata", ... ]
Notes
Do not call an array Array as is the name of the array constructor. You will lose the ability to do new Array().
I have been scanning through stackoverflow topics and couldn't find answer to the problem i am having.
What i need to do is to find object inside nested (2 depths) array by some of the values and then update other of its values. I managed to put together the finding and also setting seems to work, the problem is that lodash does return main object and it updates main object, not the nested one i actually need.
lets take this structure:
var data = [
{
name: 'I',
status: "0",
categories: [
{
name: 'I1',
status: "0",
specifics: [
{
name: "I1a",
status: "0",
}
]
}
]
}
];
i need to find object inside specifics by its name and then update its status.
so lets try simple find first:
var find = _.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]});
this is working but it returns the object with name: I so first main one.
so then if i try to update with this:
var set = _.set(_.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]}), 'status', "1");
It also does work but it updates status of I instead of what i was looking for which is I1a.
Is there a way to make finding and therefore setting to return / work on actually queried object ?
var data = [
{
name: 'I',
status: "0",
categories: [
{
name: 'I1',
status: "0",
specifics: [
{
name: "I1a",
status: "0",
}
]
}
]
}
];
var find = _.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]})
console.log('find', find);
var set = _.set(_.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]}), 'status', "1");
console.log('set', set);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
I need to iterate through the array of objects as long as the children key has values in it.
Each object in the array will have an individual children array and I need to check if the timeGroupName exists in each of the objects.
And finally, if the timeGroupName is missing in any of the objects, return something to indicate that timeGroupName does not exist. I want to use recursion here.
Sample Object:
[
{
name: "test",
timeGroupName: "NupurGroup",
type: "node",
id: 1592208617196,
children: [
{
name: "sid",
timeGroupName: "NupurGroup",
type: "node",
id: 1592210050837,
children: [
{
name: "rush",
timeGroupName: "NupurGroup",
type: "node",
id: 1592210076303,
children: []
},
{
name: "1",
timeGroupName: "NupurGroup",
type: "store",
storeId: "5c46e5fde6d3c2293e1f53b6",
id: 1592210057381,
children: []
}
],
collapsedStore: false,
collapsedGroup: false
}
],
collapsedGroup: false
}
];
You could use this recursive function:
const findMissing = data => (data || []).flatMap(item =>
("timeGroupName" in item ? [] : [item.id]).concat(findMissing(item.children))
);
This will return an array with the id values of all the objects that have no "timeGroupName" property. In your example all objects have it, so the above function would return [] for it.
I need to traverse a pretty deep object and I need to extract 12 values in different depths. My plan was to extract with the for of the values for each depth but I have a problem in the first depth.
I am a little confused about the for of behavior.
I thought this:
for (const key of Object.keys(jsonData)){
console.log(i+1);
if (isWantedValue(key))
{
artifactColl[key] = jsonData[key];
}
console.log(key, jsonData[key]);
}
for of loop would run a cycle for each key element that it finds inside the object but the loop is only running once. It prints out all necessary keys with values in the lower console.log function but it calls isWantedValue function only once.
Can please somebody explain that to me?
Object looks like this:
{ searchNGResponse:
{ totalCount: [ '420' ],
from: [ '-1' ],
count: [ '-1' ],
tooManyResults: [ 'false' ],
collapsed: [ 'false' ],
repoDetails: [ [Object] ],
data: [ [Object] ] } }
console output:
1
called with searchNGResponse
searchNGResponse { totalCount: [ '416' ],
from: [ '-1' ],
count: [ '-1' ],
tooManyResults: [ 'false' ],
collapsed: [ 'false' ],
repoDetails: [ { 'xxx': [Object] } ],
data: [ { artifact: [Object] } ] }
Edit: updated
Your jsonData object has only one key, as the console output shows: searchNGResponse.
What you want is the keys of that object, the searchNGResponse object, specifically jsonData.searchNGResponse like this
for (const key of Object.keys(jsonData.searchNGResponse)){
console.log(i+1);
if (isWantedValue(key))
{
artifactColl[key] = jsonData.searchNGResponse[key];
}
console.log(key, jsonData.searchNGResponse[key]);
}
I have this array:
var myArray = [
{ familyName: 'one', subfamilies:
[ { subfamilyName: 'subOne', subItems:
[ { name: 'subOne', code: '1' },
{ name: 'subTwo', code: '2' }
] }
]
},
{ familyName: 'two', subfamilies:
[ { subfamilyName: 'subTwo', subItems:
[ { name: 'subOne', code: '1' },
{ name: 'subTwo', code: '2' },
{ name: 'subTwo', code: '3' }
] }
]
}
]
I need to divide that array in two diferent arrays with the same length if possible (my real array is so much longer), but I am having some problems getting it done. I create 2 blank array and, with for sentence read all the items. First I push the subItems in one blank array, but cannot get the way to create a new subFamily in a blank array variable and then push the sutItems array.
How can be this done?
Thanks a lot in advance.
var myOtherArray = myArray.splice(myArray.length / 2 | 0);