I have a array like this:
[
{
id: 12,
selected: true
},
{
id: 12,
selected: true
}
]
I want only remove one of the same ID, but filter remove all, how can I only remove one object?
To remove one object out of all duplicates you could use this:
let objectIds = {};
[
{
id: 12,
selected: true
},
,
{
id: 14,
selected: true
},
{
id: 12,
selected: true
},
{
id: 12,
selected: true
},
{
id: 14,
selected: true
}
].filter((obj) => {
const oid = objectIds[obj.id];
if ( oid ) {
oid.nr += 1;
} else {
return objectIds[obj.id] = {nr: 1};
}
if (oid.nr > 1 && !oid.removed) {
oid.removed = true;
return false;
}
return true;
});
Related
listOne: [
{
id: 1,
compId: 11,
active: false,
},
{
id: 2,
compId: 22,
active: false,
},
{
id: 3,
compId: 33,
active: false,
},
]
listTwo: [
{
id: 1,
compId: 11,
active: true,
},
{
id: 2,
compId: 33,
active: false,
},
]
I have two json, here how to compare with compId key and update the active key in listOne from listTwo if compId is same.
In AngularJs I have tried with below this related link for AngularJs
But how to integrate with Angular 6 with Typescript.
And expected output is
listOne: [
{
id: 1,
compId: 11,
active: true,
},
{
id: 2,
compId: 22,
active: false,
},
{
id: 3,
compId: 33,
active: false,
},
]
You can use map and filter like this
listOne = listOne.map(item => {
let exist = listTwo.filter(c=>c.compId == item.compId)[0];
if(exist != undefined){
item.active = exist.active;
return item;
}else{
return item;
}
});
let listOne= [
{
id: 1,
compId: 11,
active: false,
},
{
id: 2,
compId: 22,
active: false,
},
{
id: 3,
compId: 33,
active: false,
},
]
let listTwo= [
{
id: 1,
compId: 11,
active: true,
},
{
id: 2,
compId: 33,
active: false,
},
]
//let arr3 = [];
listOne = listOne.map(item => {
let exist = listTwo.filter(c=>c.compId == item.compId)[0];
if(exist != undefined){
//item.id = exist.id;
item.active = exist.active;
return item;
}else{
return item;
}
});
console.log(listOne);
Try this,
// Iterate over list one
for(let item of this.listOne){
// Check if the item exist in list two
if(this.listTwo.find(i=>i.compId==item.compId)){
// Update key
item.active = this.listTwo.find(i=>i.compId==item.compId).active;
}
}
I tried and got solution for the question.
So we need to iterate two for to check each data like below.
And need to replace as need
for (const s of listOne) {
for (const r of listTwo) {
if (s.compId === r.compId) {
s.active = r.active;
break;
}
}
}
I have already mentioned the link which the answer available for AngularJS. but i'm looking for Angular 2+ Typescript.
I have an array of objects:
const guests = [
{ id: 1, rsvp: true },
{ id: 2, rsvp: false },
{ id: 3, rsvp: true },
{ id: 4, rsvp: false }
];
I would like to write a function which selects only the objects corresponding to IDs (guests) who have rsvp'd.
function selectGuests(guests, id) {
list.forEach(function(id) {
if(id.true) {
push.SelectGuests();
}
});
return selectGuests;
}
however, I am getting gibberish results.
Any help on this one or points in the right direction would be appreciated!
Cheers!
Use array.filter()
DEMO
const guests = [
{ id: 1, rsvp: true },
{ id: 2, rsvp: false },
{ id: 3, rsvp: true },
{ id: 4, rsvp: false }
];
var result = guests.filter(t=>t.rsvp);
console.log(result);
using forEach
function selectGuests(guests)
{
let result = [];
guests.forEach(function (guest) {
if (guest.rsvp) {
result.push(guest);
}
});
return result;
}
const guests = [
{ id: 1, rsvp: true },
{ id: 2, rsvp: false },
{ id: 3, rsvp: true },
{ id: 4, rsvp: false }
];
let a = selectGuests(guests);
console.log(a);
or simpler using filter method
const guests = [
{ id: 1, rsvp: true },
{ id: 2, rsvp: false },
{ id: 3, rsvp: true },
{ id: 4, rsvp: false }
];
let a = guests.filter(function(item){return item.rsvp});
console.log(a);
here is a simplified version without using es6 features.
function selectGuests(guests) {
var selectedGuests = []
// forEach gets the array elements (one by one) as first param
guests.forEach(function(guest) {
if(guest.rsvp) {
selectedGuests.push(guest)
}
})
return selectedGuests
}
// call it as follows
selectGuests(guests)
I am filtering array whenever checkboxes are checked. There are totally 7 checkboxe each is associated with an object.
here is my code,
if (this.deliveryConcession[0].checked) {
this.allItems = this.allItems.filter(fil => fil.deliveryconcession.readytoship === this.deliveryConcession[0].checked);
}
if (this.deliveryConcession[1].checked) {
this.allItems = this.allItems.filter(fil => fil.deliveryconcession.instantdownload === this.deliveryConcession[1].checked);
}
if (this.deliveryConcession[2].checked) {
this.allItems = this.allItems.filter(fil => fil.deliveryconcession.unespecifiedshipment === this.deliveryConcession[2].checked);
}
if (this.seatConcession[0].checked) {
this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking === this.seatConcession[0].checked);
}
if (this.seatConcession[1].checked) {
this.allItems = this.allItems.filter(fil => fil.seatingConcession.restrictedview === this.seatConcession[1].checked);
}
if (this.seatConcession[2].checked) {
this.allItems = this.allItems.filter(fil => fil.seatingConcession.wheelchair === this.seatConcession[2].checked);
}
if (this.seatConcession[3].checked) {
this.allItems = this.allItems.filter(fil => fil.seatingConcession.alcoholFree === this.seatConcession[3].checked);
}
here is my objects for filter,
seatConcession = [
{ id: 1, name: 'Parking pass included', checked: false },
{ id: 2, name: 'Unrestricted view', checked: false },
{ id: 3, name: 'Wheel chair accessible', checked: false },
{ id: 4, name: 'Without age restrictions', checked: false }
];
deliveryConcession = [
{ id: 1, name: 'Ready to ship(paper)', checked: false },
{ id: 2, name: 'Instant download(e-ticket)', checked: false },
{ id: 3, name: 'Unspecified shipment(paper)', checked: false }
];
how can i improve the above with simple loadash filter or another way?
let keys = [
["readytoship", "deliveryConcession"],
["instantdownload", "deliveryConcession"],
/* and so on, make sure to order */
];
this.allItems.filter(item => {
return keys.every((arr, i) => {
let [k, attr] = arr;
return item[attr][k] === this[attr][i].checked;
});
});
You will need to order the keys array appropriately. But now it's a two-liner. Other than let and the arrow functions this is all valid ES 5, no lodash required.
EDIT
Since you still haven't actually posted the relevant code this is still something of a stab in the dark, but taking your sample input of
seatConcession = [
{ id: 1, name: 'Parking pass included', checked: false },
{ id: 2, name: 'Unrestricted view', checked: false },
{ id: 3, name: 'Wheel chair accessible', checked: false },
{ id: 4, name: 'Without age restrictions', checked: false }
];
deliveryConcession = [
{ id: 1, name: 'Ready to ship(paper)', checked: false },
{ id: 2, name: 'Instant download(e-ticket)', checked: false },
{ id: 3, name: 'Unspecified shipment(paper)', checked: false }
];
And assuming you have a list of which checkboxes are checked that is ordered in the same order as the objects like so
let checked = [true, false, true, true, false /* etc */];
You want to do something like this:
let filtered = seatConcessions
.concat(deliveryConcession)
.filter((obj, i) => checked[i]);
You will have to adapt this to your specific case (again, since the sample input you put up is different than the code you wrote), but is a pattern for doing this in general.
could you please tell me why my script is broken?
It is an exercise in a Udemy lesson.
You need only returning users who have admin level access
var users = [
{ id: 1, admin: true },
{ id: 2, admin: false },
{ id: 3, admin: false },
{ id: 4, admin: false },
{ id: 5, admin: true },
];
var filteredUsers;
function isAdmin(array, property){
return array.filter(function(key){
return key[property] === 'true';
})
}
filteredUsers = isAdmin(users, 'admin');
Thank you
You need to test against a boolean value, because your data has true or false values.
return key[property] === true;
// ^^^^
function isAdmin(array, property) {
return array.filter(function (key) {
return key[property] === true;
// ^^^^
});
}
var users = [{ id: 1, admin: true }, { id: 2, admin: false }, { id: 3, admin: false }, { id: 4, admin: false }, { id: 5, admin: true }],
filteredUsers = isAdmin(users, 'admin');
console.log(filteredUsers);
)Your problem is that your are using the 3 equal sing ("===") to test the proprety. Here's a link with more details that explains the difference between :
https://stackoverflow.com/a/523650/5235299
I am trying to implement a function "markActive", which, when given a list of objects and an id, returns the same list, but with the corresponding object marked active, like for example,
var list = [
{ id: 1, active: false },
{ id: 2, active: false },
{ id: 3, active: true },
{ id: 4, active: false }
];
when function is called
markActive(list, 2);
should Return:
[
{ id: 1, active: false },
{ id: 2, active: true },
{ id: 3, active: false },
{ id: 4, active: false }
]
i tried many loops but everytime i was getting undefined,
thank you in advance.
You could iterate the array and set active with a check of the id.
function markActive(array, id) {
array.forEach(function (o) {
o.active = o.id === id;
});
return list;
}
var list = [{ id: 1, active: false }, { id: 2, active: false }, { id: 3, active: true }, { id: 4, active: false }];
console.log(markActive(list, 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use Array#forEach
var list = [{
id: 1,
active: false
}, {
id: 2,
active: false
}, {
id: 3,
active: true
}, {
id: 4,
active: false
}];
function markActive(list, id) {
list.forEach(function(el) {
if (el.id == id) {
el.active = true;
} else {
el.active = false;
}
});
}
markActive(list, 2);
console.log(list);
Set everything to inactive unless the id should be set to active then do that
function markActive(list, item) {
list.forEach(function(element) {
if (element.active) {
element.active = false;
}
if (element.id === item) {
console.log(item)
element.active = true;
}
})
}