How can I remove duplicate keys from object in javascript? [duplicate] - javascript

This question already has an answer here:
How do i group duplicate objects in an array
2 answers
How to remove duplicates objects from array in javascript?
6 answers
I am tring to remove duplicate keys from array but not able to remove here's my code i am getting like this
1: {section: "acb46bfa-0b65-e911-a957-000d3a3b9a2b", question: "2d492e19-bd65-e911-a952-000d3a3b939c"}
2: {section: "acb46bfa-0b65-e911-a957-000d3a3b9a2b", question: "1f492e19-bd65-e911-a952-000d3a3b939c"}
3: {section: "acb46bfa-0b65-e911-a957-000d3a3b9a2b", question: "21492e19-bd65-e911-a952-000d3a3b939c"}
4: {section: "acb46bfa-0b65-e911-a957-000d3a3b9a2b", question: "2b492e19-bd65-e911-a952-000d3a3b939c"}
5: {section: "acb46bfa-0b65-e911-a957-000d3a3b9a2b", question: "33492e19-bd65-e911-a952-000d3a3b939c"}
6: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "1f492e19-bd65-e911-a952-000d3a3b939c"}
7: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "9966879d-c965-e911-a957-000d3a3b9a2b"}
8: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "1b492e19-bd65-e911-a952-000d3a3b939c"}
9: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "1d492e19-bd65-e911-a952-000d3a3b939c"}
10: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "23492e19-bd65-e911-a952-000d3a3b939c"}
11: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "27492e19-bd65-e911-a952-000d3a3b939c"}
12: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "29492e19-bd65-e911-a952-000d3a3b939c"}
length: 13
__proto__: Array(0)
but i need like this
1: {section: "acb46bfa-0b65-e911-a957-000d3a3b9a2b", question: "2d492e19-bd65-e911-a952-000d3a3b939c","1f492e19-bd65-e911-a952-000d3a3b939c","21492e19-bd65-e911-a952-000d3a3b939c"}
2: {section: "68f17e95-0c65-e911-a957-000d3a3b9a2b", question: "1d492e19-bd65-e911-a952-000d3a3b939c","23492e19-bd65-e911-a952-000d3a3b939c"}

Related

Vuejs : Why my array result contains__ob__: Observer?

I'm trying to iterate over an array and do some changes. but results contains some {ob: Observer} thing. I know this is part of Vuejs reactivity system but
I want to know how to overcome it.
my code :
axios.post(route('foo.bar'), data).then((resp) => {
resp.data.forEach((value)=>{
value.path = value.path.split('\\', 1).join('\\')
});
this.pathSearchResults = _.sortedUniqBy(resp.data, 'path');
console.log(resp.data);
}
Console :
0: {__ob__: Observer}
1: {path: "root\new1", id: 3, type: "file"}
2: {path: "root\new1", id: 4, type: "file"}
3: {path: "root\new1", id: 5, type: "file"}
4: {path: "root\new1", id: 6, type: "file"}
5: {path: "root\new1", id: 7, type: "file"}
6: {__ob__: Observer}
7: {__ob__: Observer}
12: {path: "root\new1", id: 15, type: "directory"}
and main question is why some of array items is reactive and some are not

How do I get my array from firebase to have the exact same output as my static array?

So I want to move my data from an array within my JS application into Firebase so i can easily update the values, however i'm having issues getting the value to return EXACTLY as it was when it was static. It needs to be identical since i'm looping over each of the players. I'm sure its something simple i'm missing but any insight would be greatly appreciated.
This is how i've structured my static array:
// Team names and player IDs for each go here
const staticTeams = [
{
name: 'Byron',
players: ["192", "278", "176", "172", "37", "335"]
},
//etc
];
This is how i'm fetching the Firebase data:
async function getTeams(){
let teams = []
db.collection("teams").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
let data = doc.data();
teams.push(data)
// console.log(doc.id, " => ", doc.data());
});
});
return teams
};
And i'm testing by printing to the console within another function like this:
console.log("firebase", teams)
console.log("static", staticTeams)
Console outputs below:
firebase
[]
0: {name: "Mick", players: Array(6)}
1: {name: "Scott", players: Array(6)}
2: {name: "Moir", players: Array(6)}
3: {name: "Boyd", players: Array(6)}
4: {name: "Trev", players: Array(6)}
5: {name: "Tex", players: Array(6)}
6: {name: "Cail", players: Array(6)}
7: {name: "Byron", players: Array(6)}
length: 8
__proto__: Array(0)
static
(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {name: "Byron", players: Array(6)}
1: {name: "Moir", players: Array(6)}
2: {name: "Cail", players: Array(6)}
3: {name: "Boyd", players: Array(6)}
4: {name: "Mick", players: Array(6)}
5: {name: "Tex", players: Array(6)}
6: {name: "Trev", players: Array(6)}
7: {name: "Scott", players: Array(6)}
length: 8
__proto__: Array(0)
You can store an array of key-value-pairs that contains arrays as elements with a structure like this in Cloud Firestore:
Or if it has to be exactly the same structure you have now, it will look something like this:
To fetch the array from Firestore, you should do something like this:
db.collection("teams").doc("teamDocument").get().then(function(doc) {
teams = doc.data().team; //this would be the array field
});

filter() array by another array some() not working

In the following method, I need to filter the first array based on whether the items id exists in the second array. For some reason this returns empty for me though.
dialogRef.afterClosed().subscribe((airlines: AirlineModel[]) => {
console.log(airlines, this.airlines);
const updates = airlines.filter(airline => this.airlines.some(item => item.id === airline.id));
console.log(updates);
});
console.log(airlines, this.airlines); results in:
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "1", code: "AAL", name: "American Airlines"}
1: {id: "6", code: "DAL", name: "Delta Airlines"}
2: {id: "526", code: "SWA", name: "Southwest Airlines"}
3: {id: "27", code: "ASA", name: "Alaska Airlines"}
4: {id: "23", code: "FDX", name: "FedEx"}
5: {id: "205", code: "NCA", name: "Nippon Cargo"}
6: {id: "406", code: "UPS", name: "United Parcel Service"}
7: {id: "160", code: "CPA", name: "Cathay Pacific"}
8: {id: "403", code: "PAC", name: "Polar Air Cargo"}
9: {id: "992", code: "GTI", name: "Atlas Air"}
10: {id: "272", code: "CKS", name: "Kalitta Air"}
11: {id: "345", code: "NAC", name: "Northern Air Cargo"}
12: {id: "615", code: "BCS", name: "European Air Transport"}
length: 13
__proto__: Array(0)
(12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 6, code: "DAL", name: "Delta Airlines"}
1: {id: 526, code: "SWA", name: "Southwest Airlines"}
2: {id: 27, code: "ASA", name: "Alaska Airlines"}
3: {id: 23, code: "FDX", name: "FedEx"}
4: {id: 205, code: "NCA", name: "Nippon Cargo"}
5: {id: 406, code: "UPS", name: "United Parcel Service"}
6: {id: 160, code: "CPA", name: "Cathay Pacific"}
7: {id: 403, code: "PAC", name: "Polar Air Cargo"}
8: {id: 992, code: "GTI", name: "Atlas Air"}
9: {id: 272, code: "CKS", name: "Kalitta Air"}
10: {id: 345, code: "NAC", name: "Northern Air Cargo"}
11: {id: 615, code: "BCS", name: "European Air Transport"}
length: 12
__proto__: Array(0)
console.log(updates); results in:
[]length: 0__proto__: Array(0)
The airlines id property is a string, while this.airlines id property is a number. Change the comparison from === (strict) to == (abstract).
See Equality comparisons and sameness on mdn.
const airlines = [{"id":"1","code":"AAL","name":"American Airlines"},{"id":"6","code":"DAL","name":"Delta Airlines"},{"id":"526","code":"SWA","name":"Southwest Airlines"},{"id":"27","code":"ASA","name":"Alaska Airlines"},{"id":"23","code":"FDX","name":"FedEx"},{"id":"205","code":"NCA","name":"Nippon Cargo"},{"id":"406","code":"UPS","name":"United Parcel Service"},{"id":"160","code":"CPA","name":"Cathay Pacific"},{"id":"403","code":"PAC","name":"Polar Air Cargo"},{"id":"992","code":"GTI","name":"Atlas Air"},{"id":"272","code":"CKS","name":"Kalitta Air"},{"id":"345","code":"NAC","name":"Northern Air Cargo"},{"id":"615","code":"BCS","name":"European Air Transport"}];
const thisAirlines = [{"id":6,"code":"DAL","name":"Delta Airlines"},{"id":526,"code":"SWA","name":"Southwest Airlines"},{"id":27,"code":"ASA","name":"Alaska Airlines"},{"id":23,"code":"FDX","name":"FedEx"},{"id":205,"code":"NCA","name":"Nippon Cargo"},{"id":406,"code":"UPS","name":"United Parcel Service"},{"id":160,"code":"CPA","name":"Cathay Pacific"},{"id":403,"code":"PAC","name":"Polar Air Cargo"},{"id":992,"code":"GTI","name":"Atlas Air"},{"id":272,"code":"CKS","name":"Kalitta Air"},{"id":345,"code":"NAC","name":"Northern Air Cargo"},{"id":615,"code":"BCS","name":"European Air Transport"}];
const updates = airlines.filter(airline =>
thisAirlines.some(item => item.id == airline.id)
);
console.log(updates);
Instead of using abstract equality you can convert the string id to a number using + - +airlines.id or convert the number to a string using - String(this.airlines.id).
const airlines = [{"id":"1","code":"AAL","name":"American Airlines"},{"id":"6","code":"DAL","name":"Delta Airlines"},{"id":"526","code":"SWA","name":"Southwest Airlines"},{"id":"27","code":"ASA","name":"Alaska Airlines"},{"id":"23","code":"FDX","name":"FedEx"},{"id":"205","code":"NCA","name":"Nippon Cargo"},{"id":"406","code":"UPS","name":"United Parcel Service"},{"id":"160","code":"CPA","name":"Cathay Pacific"},{"id":"403","code":"PAC","name":"Polar Air Cargo"},{"id":"992","code":"GTI","name":"Atlas Air"},{"id":"272","code":"CKS","name":"Kalitta Air"},{"id":"345","code":"NAC","name":"Northern Air Cargo"},{"id":"615","code":"BCS","name":"European Air Transport"}];
const thisAirlines = [{"id":6,"code":"DAL","name":"Delta Airlines"},{"id":526,"code":"SWA","name":"Southwest Airlines"},{"id":27,"code":"ASA","name":"Alaska Airlines"},{"id":23,"code":"FDX","name":"FedEx"},{"id":205,"code":"NCA","name":"Nippon Cargo"},{"id":406,"code":"UPS","name":"United Parcel Service"},{"id":160,"code":"CPA","name":"Cathay Pacific"},{"id":403,"code":"PAC","name":"Polar Air Cargo"},{"id":992,"code":"GTI","name":"Atlas Air"},{"id":272,"code":"CKS","name":"Kalitta Air"},{"id":345,"code":"NAC","name":"Northern Air Cargo"},{"id":615,"code":"BCS","name":"European Air Transport"}];
const updates = airlines.filter(airline =>
thisAirlines.some(item => item.id === +airline.id)
);
console.log(updates);
Since your using typescript, and you're doing this comparison several times, it's better to create a Set of this.airlines string ids, band use that to compare:
const airlines = [{"id":"1","code":"AAL","name":"American Airlines"},{"id":"6","code":"DAL","name":"Delta Airlines"},{"id":"526","code":"SWA","name":"Southwest Airlines"},{"id":"27","code":"ASA","name":"Alaska Airlines"},{"id":"23","code":"FDX","name":"FedEx"},{"id":"205","code":"NCA","name":"Nippon Cargo"},{"id":"406","code":"UPS","name":"United Parcel Service"},{"id":"160","code":"CPA","name":"Cathay Pacific"},{"id":"403","code":"PAC","name":"Polar Air Cargo"},{"id":"992","code":"GTI","name":"Atlas Air"},{"id":"272","code":"CKS","name":"Kalitta Air"},{"id":"345","code":"NAC","name":"Northern Air Cargo"},{"id":"615","code":"BCS","name":"European Air Transport"}];
const thisAirlines = [{"id":6,"code":"DAL","name":"Delta Airlines"},{"id":526,"code":"SWA","name":"Southwest Airlines"},{"id":27,"code":"ASA","name":"Alaska Airlines"},{"id":23,"code":"FDX","name":"FedEx"},{"id":205,"code":"NCA","name":"Nippon Cargo"},{"id":406,"code":"UPS","name":"United Parcel Service"},{"id":160,"code":"CPA","name":"Cathay Pacific"},{"id":403,"code":"PAC","name":"Polar Air Cargo"},{"id":992,"code":"GTI","name":"Atlas Air"},{"id":272,"code":"CKS","name":"Kalitta Air"},{"id":345,"code":"NAC","name":"Northern Air Cargo"},{"id":615,"code":"BCS","name":"European Air Transport"}];
const thisAirlinesSet = thisAirlines.reduce((s, o) => s.add(String(o.id)), new Set);
const updates = airlines.filter(airline =>
thisAirlinesSet.has(airline.id)
);
console.log(updates);
The problem is that the filter method is using === operator which compares also the variable types if im correct.Both arrays have different id types, this.airlines have id of type number and the airlines have the id of type string.Replacing === operator to == should solve your issue
You are using "===" to compare, so its must be the same type of comparison!
In this case, you can use "==" to solve this problem.
const updates = airlines.filter(airline => this.airlines.some(item => +item.id === +airline.id));
You are using === which means type is also checked, so add a + sign to parse it to int and safe checking.

Sort JSON entries in a numerical order with specification

I have a JSON, which I successfully can sort numerical.
data["example"].sort(function (a, b) {
return a["one"] - b["two"];
});
// Output:
0: {location: "0"}
1: {location: "0"}
2: {location: "0"}
3: {location: "0"}
4: {location: "0"}
5: {location: "0"}
6: {location: "0"}
7: {location: "0"}
8: {location: "1"}
9: {location: "2"}
10: {location: "3"}
11: {location: "4"}
12: {location: "5"}
13: {location: "6"}
14: {location: "7"}
However, I want it to sort starting from 1 to ∞, and after that, append all the 0's.
Like this:
0: {location: "1"}
1: {location: "2"}
2: {location: "3"}
3: {location: "4"}
4: {location: "5"}
5: {location: "6"}
6: {location: "7"}
7: {location: "0"}
8: {location: "0"}
9: {location: "0"}
10: {location: "0"}
11: {location: "0"}
12: {location: "0"}
13: {location: "0"}
14: {location: "0"}
I am sure, there is an easy solution to this, but I can't find anything about that specific numerical sort.
You can check if location is 0, if it is, use Infinity to sort to make sure all location will be placed in the end of the array.
let arr = [{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"1"},{"location":"2"},{"location":"3"},{"location":"4"},{"location":"5"},{"location":"6"},{"location":"7"}];
arr.sort(function(a, b) {
return (a.location === "0" ? Infinity : a.location) - (b.location === "0" ? Infinity : b.location);
});
console.log(arr);
// Output:
// [{"location":"1"},{"location":"2"},{"location":"3"},{"location":"4"},{"location":"5"},{"location":"6"},{"location":"7"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"},{"location":"0"}];
after get output run for
for (let i = 0; i < array.length; i++) {
array[i].location = i+1;
}
or use map like
let counter = 1;
data["example"].sort(function (a, b) {
return a["one"] - b["two"];
}).map(c=> {c.location = counter++;return c});

i'm working on the autocomplete for displaying the city name and also the city id in hidden,

Im getting the response as:
0: {id: "190", name: "Elurupadu"}
1: {id: "1230", name: "Sendhwa"}
2: {id: "1262", name: "Multai"}
3: {id: "1480", name: "Kherwara"}
4: {id: "2225", name: "Mandi"}
5: {id: "519", name: "Thamarassery"}
6: {id: "208", name: "Ayyampettai"}
7: {id: "5519", name: "Udaipur"}
8: {id: "26977", name: "Siddapura(Udupi)"}
9: {id: "1151", name: "Murthal"}
10: {id: "327", name: "Pillayarpatti"}
11: {id: "2022", name: "Siddapura"}
12: {id: "738", name: "Therthally"}
13: {id: "1112", name: "Chandigarh"}
14: {id: "2358", name: "Kadodra"}
...............
I want to convert it as:
[ {id: "960",name: "Elurupadu"},
{id: "1230",name: "Senhwa"},
{id: "1262",name: "Multai"},
...
];
can any one help me for sorting out, im using javascript.
var dt=[];
dt.push({id: "190", name: "Elurupadu"})
dt.push({id: "1230", name: "Sendhwa"})
dt.push({id: "1262", name: "Multai"})
dt.push({id: "1480", name: "Kherwara"})
dt.push({id: "2225", name: "Mandi"})
dt.push({id: "519", name: "Thamarassery"})
dt.push({id: "208", name: "Ayyampettai"})
dt.push({id: "5519", name: "Udaipur"})
dt.push({id: "26977", name: "Siddapura(Udupi)"})
dt.push({id: "1151", name: "Murthal"})
dt.push({id: "327", name: "Pillayarpatti"})
dt.push({id: "2022", name: "Siddapura"})
dt.push({id: "738", name: "Therthally"})
dt.push({id: "1112", name: "Chandigarh"})
dt.push({id: "2358", name: "Kadodra"})
//convert
var result=JSON.stringify(dt);
console.log(result);
/*[{"id":"190","name":"Elurupadu"},{"id":"1230","name":"Sendhwa"},{"id":"1262","name":"Multai"},{"id":"1480","name":"Kherwara"},{"id":"2225","name":"Mandi"},{"id":"519","name":"Thamarassery"},{"id":"208","name":"Ayyampettai"},{"id":"5519","name":"Udaipur"},{"id":"26977","name":"Siddapura(Udupi)"},{"id":"1151","name":"Murthal"},{"id":"327","name":"Pillayarpatti"},{"id":"2022","name":"Siddapura"},{"id":"738","name":"Therthally"},{"id":"1112","name":"Chandigarh"},{"id":"2358","name":"Kadodra"}]
*/

Resources