Flat array without using FlatMap [duplicate] - javascript

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 years ago.
I've got array model and I want to use its values array separetaly, since flatMap is not available in my angular application without adding "esnext" to tsconfig.json file I was wondering if it's possible to do the same result without flatMap. Here's my current result:
var model= [
{
"values": [
{
"colId": 1,
"value": 7086083333.333333
},
{
"colId": 2,
"value": null
},
],
"rowId": 0,
},
{
"values": [
{
"colId": 1,
"value": null
},
],
"rowId": 1,
"rowHeader": ""
},
{
"values": [
{
"colId": 1,
"value": null
},
{
"colId": 2,
"value": null
},
],
"rowId": 2,
"rowHeader": ""
}
]
const data = model.flatMap((wm) => wm.values);
console.log(data);

You can use reduce() method to do that.
var model = [{ "values": [{ "colId": 1, "value": 7086083333.333333 }, { "colId": 2, "value": null }, ], "rowId": 0, }, { "values": [{ "colId": 1, "value": null }, ], "rowId": 1, "rowHeader": "" }, { "values": [{ "colId": 1, "value": null }, { "colId": 2, "value": null }, ], "rowId": 2, "rowHeader": "" } ];
const data = model.reduce((arr, currentValue) => {
return arr.concat(currentValue.values);
}, []);
console.log(data);

Related

javascript extract and convert into new array

I have the following array
[ {
"contactId": "a87d096gd5fuop",
"firstName": "John Doe",
"registrationTypes": {
"selectedOptions": [
{
}
],
"subTotal": 1620.003
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"events": {
"selectedOptions": [
{
"id": "1",
"name": "T1",
"value": "4550006:3",
},
{
"id": "2",
"name": "T2",
"value": "4550005:3",
},
{
"id": "3",
"name": "T3",
"value": "4550003:3",
}
],
"subTotal": 135.003
},
"freeNetworkingFunctions": {
},
"total": 1755.0059999999999
},
{
"contactId": "a097f",
"firstName": "David",
"registrationTypes": {
"selectedOptions": [
{}
],
"subTotal": 899.998
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"member": {
"selectedOptions": [
{
}
],
"subTotal": 228.8
},
"events": {
"selectedOptions": [
{
"id": "4",
"name": "T4",
"value": "4550002:2",
},
{
"id": "5",
"name": "T5",
"value": "4550001:2",
},
{
"id": "6",
"name": "T6",
"value": "4550003:2",
}
],
"subTotal": 135.003
},
"total": 1263.801
}
]
From the above array, I want to extract events, loop all the data and get only values. So my new array should be something like this:
[ {
"contactId": "a87d096gd5fuop",
"firstName": "John Doe",
"registrationTypes": {
"selectedOptions": [
{
}
],
"subTotal": 1620.003
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"events": [
"4550006:3"
"4550005:3",
"4550003:3",
],
},
"freeNetworkingFunctions": {
},
"total": 1755.0059999999999
},
{
"contactId": "a097f",
"firstName": "David",
"registrationTypes": {
"selectedOptions": [
{}
],
"subTotal": 899.998
},
"foo1": {
"selectedOptions": [
],
"subTotal": 0
},
"member": {
"selectedOptions": [
{
}
],
"subTotal": 228.8
},
"events": [
"4550004:2"
"4550008:3",
"4550003:3",
],
"subTotal": 135.003
},
"total": 1263.801
}
]
So it should return the original array, however, events value data should be in one array.
var arr = [];
var r(var i=0;i<data.length;i++){
data.push(arr[i].value);
}
var newData = [...data, arr]
However, this doesn't work. Any help would be highly appreciated.
Use map twice - once on the dataset to iterate over the objects, and within that map to get an array of values from the selectedOptions.
const data=[{contactId:"a87d096gd5fuop",firstName:"John Doe",registrationTypes:{selectedOptions:[{}],subTotal:1620.003},foo1:{selectedOptions:[],subTotal:0},events:{selectedOptions:[{id:"1",name:"T1",value:"4550006:3"},{id:"2",name:"T2",value:"4550005:3"},{id:"3",name:"T3",value:"4550003:3"}],subTotal:135.003},freeNetworkingFunctions:{},total:1755.0059999999999},{contactId:"a097f",firstName:"David",registrationTypes:{selectedOptions:[{}],subTotal:899.998},foo1:{selectedOptions:[],subTotal:0},member:{selectedOptions:[{}],subTotal:228.8},events:{selectedOptions:[{id:"4",name:"T4",value:"4550002:2"},{id:"5",name:"T5",value:"4550001:2"},{id:"6",name:"T6",value:"4550003:2"}],subTotal:135.003},total:1263.801}];
const out = data.map(obj => {
// Destructure the selected options from the
// rest of each object
const { events: { selectedOptions }, ...rest } = obj;
// `map` over the options to just get an array of values
const events = selectedOptions.map(option => {
return option.value;
});
// Return a new object with the new events property
// combined with the other properties again
return { ...rest, events };
});
console.log(out);
Additional documentation
Destructuring assignment
Rest parameters
Spread syntax

Filter complex array in react native?

I have array .
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
I tried following the code to filter it. and no output show
arr.map((item,idx) => (
console.log(item.data.games.gamename)
)
))
I want to print all game name eg.
cricket
football
videogames
volleyball
We can use flatMap() to do it
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
// multiple flatMap chain invocation seems ugly,waiting for more elegant solution
let result = arr.flatMap(a => a.data).flatMap(a => a.games).flatMap(a => a.gamename)
console.log(result)
Data is a array and so is games:
const arr = [
{
status: "success",
data: [
{
name: "user1",
games: [
{
id: 1,
gamename: "cricket",
},
{
id: 2,
gamename: "football",
},
],
},
{
name: "user1",
games: [
{
id: 1,
gamename: "videogames",
},
{
id: 2,
gamename: "volleyball",
},
],
},
],
},
];
arr.map((item) => {
item.data.map((item) => {
item.games.map((item) => {
console.log(item.gamename);
});
});
});
Try out this code, it will return only game names, you can change the join if don't need comma (,)
Output :
"cricket,football,videogames,volleyball"
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}];
console.log(JSON.stringify(arr.filter(e=>e.status=="success").map(e=>e.data.map(f=>f.games.map(g=>g.gamename)).join(",")).join(",")));

Find user details by ID in nested object nodejs | javascript

How to find name using id. means iterate object. create a function const searchName =()=>{}
suppose if pass 3 in function so I'd want to show .... what the name of user like this
const data = [{
"service": [
"BUSINESS",
"LEGAL",
"FINANCE",
"ADVERTISEMENT"
],
"service1": [
{ "id": 1, "name": "a" },
{ "id": 2, "name": "b" },
{ "id": 3, "name": "c" },
{ "id": 4, "name": "d" },
],
"service2": [
{ "id": 5, "name": "e" },
{ "id": 6, "name": "f" },
{ "id": 7, "name": "g" },
{ "id": 8, "name": "h" },
],
"service3": [
{ "id": 9, "name": "i" },
{ "id": 10, "name": "j" },
{ "id": 11, "name": "k" },
{ "id": 12, "name": "l" },
],
"service4": [
{ "id": 13, "name": "m" },
{ "id": 14, "name": "n" },
{ "id": 15, "name": "o" },
{ "id": 16, "name": "p" },
],
}
]
suppose user pass 3 so I want to return { "id": 3, "name": "c" } like this.
I'm trying to iterate this and find the name of the user by id but I didn't understand this iteration so I need your help.
check this code.... Enter any id number
const data = [{
"service": [
"BUSINESS",
"LEGAL",
"FINANCE",
"ADVERTISEMENT"
],
"service1": [
{ "id": 1, "name": "a" },
{ "id": 2, "name": "b" },
{ "id": 3, "name": "c" },
{ "id": 4, "name": "d" },
],
"service2": [
{ "id": 5, "name": "e" },
{ "id": 6, "name": "f" },
{ "id": 7, "name": "g" },
{ "id": 8, "name": "h" },
],
"service3": [
{ "id": 9, "name": "i" },
{ "id": 10, "name": "j" },
{ "id": 11, "name": "k" },
{ "id": 12, "name": "l" },
],
"service4": [
{ "id": 13, "name": "m" },
{ "id": 14, "name": "n" },
{ "id": 15, "name": "o" },
{ "id": 16, "name": "p" },
],
}]
var itemobj = ''
const searchName =(val)=>{
console.log('searchname')
data.map((item)=>{
let obj = Object.keys(item)
obj.map((data)=>{
let inrdata = item[data]
inrdata.map((initem)=>{
let lastdata = initem.id===val?itemobj=initem:null
})
})
})
}
searchName(3)
console.log(itemobj)
function searchName(id) {
let result = null;
for (const [key, value] of Object.entries(data)) {
if (key === "service") continue
result = value.filter(obj => {
return obj.id === id
})
if (result) break
}
return result ? result[0] : null
}
I iterate through keys, I just skip "service" one since it's not revelant.
Then, I filter the "serviceN" array, it will return an array of object (only one if found, empty array if not found).
If it's found, we stop iterating.
Then we return either the first (and logically only element) or null if not found
You could use a combination of flat and find to get get the user by id
function searchName(id) {
return data
.flatMap((item) => Object.values(item))
.flat()
.find((user) => user.id === id);
}
const result = searchName(3); // { id: 3, name: 'c' } | undefined

angularjs md checkbox get the checked values

<md-checkbox ng-repeat="primaryPrograms in ctrl.primaryProgramStudies" ng-model="ctrl.primaryProgramStudiesSelected[primaryPrograms.id]">
{{primaryPrograms.name}}
</md-checkbox>
Selected Checbox :
{{ctrl.primaryProgramStudiesSelected | json}}
Output i am getting :
Selected Checbox :
[null,true,true,true,null,true,null,true,null,true,null,null,true]
How can i get the List of Checked Values.
You can filter the original array ctrl.primaryProgramStudies based on whether the same index on ctrl.primaryProgramStudiesSelected has true
var ctrl = {};
ctrl.primaryProgramStudies = [{
"name": "test0",
"id": 0
},
{
"name": "test1",
"id": 1
},
{
"name": "test2",
"id": 2
},
{
"name": "test3",
"id": 3
},
{
"name": "test4",
"id": 4
},
{
"name": "test5",
"id": 5
},
{
"name": "test6",
"id": 6
},
{
"name": "test7",
"id": 7
},
{
"name": "test8",
"id": 8
},
{
"name": "test9",
"id": 9
},
{
"name": "test10",
"id": 10
},
{
"name": "test11",
"id": 11
},
{
"name": "test12",
"id": 12
}
]
ctrl.primaryProgramStudiesSelected = [null, true, true, true, null, true, null, true, null, true, null, null, true]
ctrl.selectedValues = ctrl.primaryProgramStudies.filter(function(obj, index) {
return ctrl.primaryProgramStudiesSelected[index] === true
})
console.log(ctrl.selectedValues)
You can use filter method, which accepts as a parameter a callback method.
The filter() method creates a new array with all elements that pass the test implemented by the provided(callback) function.
var array=[null,true,true,true,null,true,null,true,null,true,null,null,true];
ctrl.primaryProgramStudies.filter(function(item,index){
return array[index]==true;
});
Short example
var ctrl = {};
ctrl.primaryProgramStudies = [{
"name": "program0"
},
{
"name": "program1"
},
{
"name": "program2"
},
{
"name": "program3"
},
{
"name": "program4"
},
{
"name": "program5"
},
{
"name": "program6"
},
{
"name": "program7"
},
{
"name": "program8",
},
{
"name": "program9",
},
{
"name": "program10",
},
{
"name": "program11",
},
{
"name": "program12"
}
]
ctrl.primaryProgramStudiesSelected =[null,true,true,true,null,true,null,true,null,true,null,null,true];
var result=ctrl.primaryProgramStudies.filter(function(item,index){
return ctrl.primaryProgramStudiesSelected[index]==true;
});
console.log(result)

make a json format with angularjs

Hi i want to use a treeView in my angularjs, the data recieved from the server is:
[
{
"vehiculeid": 1,
"name": "ggg",
"id": 1,
"group": "TGV"
},
{
"vehiculeid": 5,
"name": "eee",
"id": 5,
"group": "TGV"
},
{
"vehiculeid": 6,
"name": "tru123",
"id": 8,
"group": "TGV"
},
{
"vehiculeid": 2,
"name": "aqs",
"id": 3,
"group": "TCF"
}
]
How can i make the data like the folowing data so that i can use it in treeView Component, this is that format which i want to got:
treedata_avm = [{
group: 'TGV',
children: [{
name: 'ggg',
data: {
vehiculeid: 1
}
}, {
name: 'eee',
data: {
vehiculeid: 5
}
}, {
name: 'tru123',
data: {
vehiculeid: 6
}
}]
},{
group: 'TCF',
children: [{
name: 'aqs',
data: {
vehiculeid: 2
}
}]
}]
How i can do that with javascript or angularjs to get this format?
P.S:
the data recieved from the server is dynamic.
Here's the code:
var test = [
{
"vehiculeid": 1,
"name": "ggg",
"id": 1,
"group": "TGV"
},
{
"vehiculeid": 5,
"name": "eee",
"id": 5,
"group": "TGV"
},
{
"vehiculeid": 6,
"name": "tru123",
"id": 8,
"group": "TGV"
},
{
"vehiculeid": 2,
"name": "aqs",
"id": 3,
"group": "TCF"
}
];
var test2 = {};
for(var i in test) {
if(typeof test2[test[i]['group']] === 'undefined') {
test2[test[i]['group']] = [];
}
test2[test[i]['group']].push({name: test[i]['name'], data: {vehiculeid: test[i]['vehiculeid']}})
}
var treedata_avm = [];
for(var j in test2) {
var final = {};
final['group'] = j;
final['children'] = test2[j];
treedata_avm.push(final)
}
console.log(treedata_avm)

Categories