Organise element in a array - javascript

i'm searching a smart way to reoganise an array by a element inside it:
In entry i've got:
[{"name": "brevet",
"country": "fr"
},{
"name": "bac",
"country": "fr"
},{
"name": "des",
"country": "ca"
},{
"name": "dep",
"country": "ca"
}{
"name": "other",,
"country": "other"}]
I want to reorganize my array by country to have this in my output:
[{
"name": "fr",
"degrees": [
{
"name": "brevet",
"country": "fr"
},{
"name": "bac",
"country": "fr"
}]
},{
"name": "ca",
"degrees": [{
"name": "des",
"country": "ca"
},{
"name": "dep",
"country": "ca"
}]
},{
"name": "other",
"degrees": [{
"name": "other",
"country": "other"
}]
}]
For this i write a dirty function, but it seems to me there is a better way but i don't see how. If someone can ligth my brain in a better way to do this i'll be helpfull
private organizeDegrees(degrees: Array<SubscriptionFieldInterface>) {
let degreesByCountry = new Array();
let storeIndex = new Array();
degrees.map(degree => {
let index = null;
storeIndex.find((element, idx) => {
if (element === degree.country) {
index = idx;
return true;
}
});
if (index === null) {
index = degreesByCountry.length;
let newEntry = {
'name': degree.country,
'degrees': new Array()
};
storeIndex.push(degree.country);
degreesByCountry.push(newEntry);
}
degreesByCountry[index].degrees.push(degree);
});
return degreesByCountry;
}
thank's

You can group the array and map the object using Object.keys:
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
var grouped = groupBy(array, "country");
var mappedArray = Object.keys(grouped).map(key => ( {name: key, degrees: grouped [key]} ));

And one more way:
arr = [ /* your array */ ];
arr = Object.values(arr.reduce((ac, el) => {
if(!ac[el.country]) ac[el.country] = {"name": el.country, "degrees": []}
ac[el.country].degrees.push(el);
return ac
}, {}))
console.log(arr) // formated

Another solution, which also handles 'id' => '#id' mapping:
const a = [{"name":"brevet","country":"fr"},{"name":"bac","country":"fr"},{"id":73,"name":"des","country":"ca"},{"name":"dep","country":"ca"},{"name":"other","country":"other"}];
const r = [...new Set(a.map(({country}) => country))] // list of unique country names
.map(c => Object.assign({name: c}, // for each country
{degrees: a.filter(x => x.country === c).map(y => Object.keys(y).includes('id') // handle 'id' => '#id' mutation
? {'#id': "/subscription_fields/" + y.id, name: y.name, country: y.country}
: y)
}))
console.log(r)

This is purely ES6, and quite terse, but possibly less readable. Also, it doesn't add the "#id": "/subscription_fields/83", which could be added as a post process:
const groupByKey = (arr, key) => [...arr.reduce((acc, deg) =>
acc.set(deg[key], {name: deg[key], degrees: [ ...(acc.get(deg[key]) || {degrees: []}).degrees, deg]})
, new Map()).values()];
console.log(groupByKey(degrees, 'country'));

You could use a hash table and collect all values in an object. For getting the result array, you need to push the object only once.
var data = [{ name: "brevet", country: "fr" }, { name: "bac", country: "fr" }, { id: 73, name: "des", country: "ca" }, { name: "dep", country: "ca" }, { name: "other", country: "other" }],
result = data.reduce(function (hash) {
return function (r, a) {
if (!hash[a.country]) {
hash[a.country] = { name: a.country, degrees: [] };
r.push(hash[a.country]);
}
hash[a.country].degrees.push({ name: a.name, country: a.country });
return r;
};
}(Object.create(null)), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Related

create new city state object

I would like to transform my initial data to desired result, I am struggling with pushing the cities to the array and making sure name key is unique.
initial data
[
{ "city": "Abbeville", "state": "Louisiana" },
{ "city": "Aberdeen", "state": "Maryland" },
{ "city": "Aberdeen", "state": "Mississippi" },
{ "city": "Aberdeen", "state": "South Dakota" },
{ "city": "Aberdeen", "state": "Washington" },
{ "city": "Abilene", "state": "Texas" },
{ "city": "Abilene", "state": "Kansas" },
{ "city": "Abingdon", "state": "Virginia" },
{ "city": "Abington", "state": "Massachusetts" },
{ "city": "Abington", "state": "Massachusetts" },
]
code
let newCityStateObject = cities.map((item, index) => {
console.log("item ", item);
if (item) {
let object = {};
let citiesArray = [];
//set state and create empty array
if (object[item.state] === undefined) {
object.name = item.state;
object.cities = [].push(item.city);
} else {
//key exists so just push to array
if (object[item.state]) {
object.cities.push(item.city);
}
}
console.log("end ", object);
return object;
}
});
my result right now
[
{ state: 'Louisiana', cities: [] },
{ state: 'Maryland', cities: [] },
{ state: 'Mississippi', cities: [] },
{ state: 'South Dakota', cities: [] },
{ state: 'Washington', cities: [] },
{ state: 'Texas', cities: [] },
{ state: 'Kansas', cities: [] },
{ state: 'Virginia', cities: [] },
]
desired result
[
{
"name": "Kentucky",
"cities": [
"Louisville/Jefferson County",
"Lexington-Fayette",
"Bowling Green",
"Owensboro",
"Covington"
]
},
{
"name": "Maryland",
"cities": [
"Baltimore",
"Frederick",
"Rockville",
"Gaithersburg",
"Bowie",
"Hagerstown",
"Annapolis"
]
}
]
Any help/tips or pointing in the right direction to solve this would be greatly appreciated.
You’re basically grouping cities by their state. First of all, array.map is not the proper method for this problem, cus when you group, the input item’s number might not match with the output’s. array.reduce is better option.
let newCityStateObject = cities.reduce((acc, item, index) => {
if (item) {
// this object has state as key, and the desired output array’s item as value
const object = acc.obj;
// if state not found, create new record
if (object[item.state] === undefined) {
const record = { name: item.state, cities: [] }
object[item.state] = record;
acc.array.push(record);
}
const record = object[item.state];
record.cities.push(item.city);
}
return acc;
}, { obj: {}, array: [] }).array;
I like the answer suggested by #hackape.
Here is one more way to consider:
let newCityStateObject = () => {
const statesArr = Array.from(cities, (item) => item.state);
// Remove state duplications
const filteredStates = [...new Set(statesArr)];
// Initializing the name and cities object and array
let result = filteredStates.map((item) => {
return { name: item, cities: [] };
});
// For each cite item, fetch the city to its corresponding result by state
cities.forEach((item) =>
result
.find((element) => element.name === item.state)
.cities.push(item.city)
);
return result;
}

Sort javascript array of objects on subobject key

I have an array of objects containing sub-objects like this;
var array=[
{
"EmployeeName": "John",
"Experience": "12",
"data":{
"Technology":"SharePoint"
}
},
{
"EmployeeName": "Charles",
"Experience": "9",
"data":{
"Technology":"ASPNET"
}
},
{
"EmployeeName": "Jo",
"Experience": "3",
"data":{
"Technology":"PHP"
}
},
{
"EmployeeName": "Daine",
"Experience": "7",
"data":{
"Technology":"javascript"
}
},
{
"EmployeeName": "Zain",
"Experience": "6",
"data":{
"Technology":"Unity"
}
}
];
Now, I want to sort this array based on the sub-objects key "Technology". I am using the example from here: https://jsfiddle.net/KSPriyaranjan/4Lzr0qux
This does the job when it sorts from a base object, but how do I get this to work:
function GetSortOrder(prop){
return function(a,b){
if( a[prop] > b[prop]){
return 1;
}else if( a[prop] < b[prop] ){
return -1;
}
return 0;
}
}
array.sort( GetSortOrder("data.Technology") );
document.write("<br><br> Sorted Technology Names : <br>");
for (var item in array) {
document.write("<br>"+array[item].Technology);
}
This produces this:
Sorted Technology Names :
undefined
undefined
undefined
undefined
Hope someone can help in this matter and thanks in advance :-)
undefined
If the property passed contains any .s, use reduce to navigate to the last nested value before comparison:
const GetSortOrder = (accessor) => {
const getVal = accessor.includes('.')
? outer => accessor.split('.').reduce((a, prop) => a[prop], outer)
: outer => outer;
return (a, b) => getVal(a).localeCompare(getVal(b));
};
var array=[
{
"EmployeeName": "John",
"Experience": "12",
"data":{
"Technology":"SharePoint"
}
},
{
"EmployeeName": "Charles",
"Experience": "9",
"data":{
"Technology":"ASPNET"
}
},
{
"EmployeeName": "Jo",
"Experience": "3",
"data":{
"Technology":"PHP"
}
},
{
"EmployeeName": "Daine",
"Experience": "7",
"data":{
"Technology":"javascript"
}
},
{
"EmployeeName": "Zain",
"Experience": "6",
"data":{
"Technology":"Unity"
}
}
];
const GetSortOrder = (accessor) => {
const getVal = accessor.includes('.')
? outer => accessor.split('.').reduce((a, prop) => a[prop], outer)
: outer => outer;
return (a, b) => getVal(a).localeCompare(getVal(b));
};
array.sort( GetSortOrder("data.Technology") );
console.log(array);
The conditional isn't strictly necessary (though you may consider it to make the intent clearer), you could also do
const getVal = outer => accessor.split('.').reduce((a, prop) => a[prop], outer)
Technology is not a property of your object. It is a property of your object.data.
Try changing
javascript array[item].Technology
to
array[item].data.Technology
You could add a function for getting the value by keeping your commparing approach.
function GetSortOrder(prop) {
const
keys = prop.split('.'),
getValue = object => keys.reduce((o, k) => o[k], object);
return function(a, b) {
const
left = getValue(a),
right = getValue(b);
if (left > right) return 1;
else if (left < right) return -1;
return 0;
}
}
var array = [{ EmployeeName: "John", Experience: "12", data: { Technology: "SharePoint" } }, { EmployeeName: "Charles", Experience: "9", data: { Technology: "ASPNET" } }, { EmployeeName: "Jo", Experience: "3", data: { Technology: "PHP" } }, { EmployeeName: "Daine", Experience: "7", data: { Technology: "javascript" } }, { EmployeeName: "Zain", Experience: "6", data: { Technology: "Unity" } }];
array.sort(GetSortOrder("data.Technology"));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to group array items based on multiple keys in Javascript / Jquery

I have an Javascript array as below
var data = [{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Line_No": "10",
"Line_Issue": "Qty not available",
"Serial_Number": "72CEP92"
},
{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Line_No": "20",
"Line_Issue": "contact info missing",
"Serial_Number": "2198IE3"
}];
I want to group my array by multiple keys. The result should look as below :
var data = [{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Lines" : [
{"Line_No": "10",
"Line_Issue": "Qty not available",
"Serial_Number": "72CEP92"},
{"Line_No": "20",
"Line_Issue": "contact info missing",
"Serial_Number": "2198IE3"}]
}];
So basically, I will group by Ticket_id, Order_Issue and Region. And I will add Items subarray where I will store all the Line_No, Line_Issue and Serial_Number.
I succeeded to group only by one key let's say by Ticket_id as the below example. When I add another key to my object obj[item."Order_Issue] = obj[item."Order_Issue] || [];, the group by will be done only on that key and ignore the previous key. Any suggestions please what am I doing wrong and how to get the exact format I want. Thank you.
var data = [{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Line_No": "10",
"Line_Issue": "Qty not available",
"Serial_Number": "72CEP92"
},
{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Line_No": "20",
"Line_Issue": "contact info missing",
"Serial_Number": "2198IE3"
},
{
"Ticket_id": "239267",
"Order_Issue": "Online Payment",
"Region": "EU",
"Line_No": "10",
"Line_Issue": "card expired",
"Serial_Number": "21BBF03"
}];
var group_Line_No = data.reduce(function (obj, item) {
obj[item.Ticket_id] = obj[item.Ticket_id] || [];
obj[item.Ticket_id].push(item.Line_No);
return obj;
}, {});
console.log(group_Line_No);
var groups = Object.keys(group_Line_No).map(function (key) {
return {Ticket_id: key, Line_No: group_Line_No[key]};
});
console.log(groups);
If you want a dynamic solution for any mix of keys you could use reduce method and inside use for...of loop and check if the current property is key to group by or should be added to new object in Lines array.
var data = [{"Ticket_id":"239248","Order_Issue":"SAP","Region":"EU","Line_No":"10","Line_Issue":"Qty not available","Serial_Number":"72CEP92"},{"Ticket_id":"239248","Order_Issue":"SAP","Region":"EU","Line_No":"20","Line_Issue":"contact info missing","Serial_Number":"2198IE3"},{"Ticket_id":"239267","Order_Issue":"Online Payment","Region":"EU","Line_No":"10","Line_Issue":"card expired","Serial_Number":"21BBF03"}]
const groupBy = keys => data.reduce((r, e) => {
const key = keys.map(k => e[k]).join('|');
const obj = {}
for (let [k, v] of Object.entries(e)) {
if (!r[key]) r[key] = {Lines: []}
if (keys.includes(k)) {
r[key][k] = v
} else {
obj[k] = v
}
}
r[key].Lines.push(obj)
return r;
}, {});
const result = groupBy(['Ticket_id', 'Order_Issue', 'Region'])
console.log(Object.values(result))
Use a similar method, but combine all the key properties into the keys of the intermedia result object.
var data = [{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Line_No": "10",
"Line_Issue": "Qty not available",
"Serial_Number": "72CEP92"
},
{
"Ticket_id": "239248",
"Order_Issue": "SAP",
"Region": "EU",
"Line_No": "20",
"Line_Issue": "contact info missing",
"Serial_Number": "2198IE3"
},
{
"Ticket_id": "239267",
"Order_Issue": "Online Payment",
"Region": "EU",
"Line_No": "10",
"Line_Issue": "card expired",
"Serial_Number": "21BBF03"
}
];
var group_Line_No = data.reduce(function(obj, item) {
let key = `${item.Ticket_id}.${item.Order_Issue}.${item.Region}`;
obj[key] = obj[key] || {
Ticket_id: item.Ticket_id,
Order_Issue: item.Order_Issue,
Region: item.Region,
Lines: []
};
obj[key].Lines.push({
Line_No: item.Line_No,
Line_Issue: item.Line_Issue,
Serial_Number: item.Serial_Number
});
return obj;
}, {});
var groups = Object.values(group_Line_No);
console.log(groups);
Your on the right track.
You can create an id by combining the Ticket_id, Order_Issue, Region when processing an entry. Then push the line data.
const groupEntries = arr =>
Object.values(
arr.reduce(
(
obj,
{ Ticket_id, Order_Issue, Region, Line_No, Line_Issue, Serial_Number }
) => {
const id = [Ticket_id, Order_Issue, Region].join('#') // unique id
obj[id] = obj[id] || { Ticket_id, Order_Issue, Region, Lines: [] }
obj[id].Lines.push({ Line_No, Line_Issue, Serial_Number }) // push new line data
return obj
},
{}
)
)
const data = [
{
Ticket_id: '239248',
Order_Issue: 'SAP',
Region: 'EU',
Line_No: '10',
Line_Issue: 'Qty not available',
Serial_Number: '72CEP92',
},
{
Ticket_id: '239248',
Order_Issue: 'SAP',
Region: 'EU',
Line_No: '20',
Line_Issue: 'contact info missing',
Serial_Number: '2198IE3',
},
{
Ticket_id: '239267',
Order_Issue: 'Online Payment',
Region: 'EU',
Line_No: '10',
Line_Issue: 'card expired',
Serial_Number: '21BBF03',
},
]
const result = groupEntries(data)
console.log(result)

Match two object keys and display another object key value in angular 4

i have two objects like this
languages = [
{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
]
and another is
user = [
{
name: "john",
language: "eng",
country: "US"
}
];
what i have to do is, match iso_639_2_code to language of user then, i have to display Language name not code from languages. basically both are different api, and i have no idea how to do it this in angular 4.
here's a link what i am trying https://stackblitz.com/edit/angular-9k2nff?file=app%2Fapp.component.ts
Use array find:
var languages = [
{"name": "english", "iso_639_2_code": "eng"},
{"name": "esperanto","iso_639_2_code": "epo"},
{"name": "estonian","iso_639_2_code": "est"}
];
var user = [{name: "john",language: "eng",country: "US"}];
var language = languages.find(l => l.iso_639_2_code === user[0].language);
var languageName = language && language.name; // <-- also prevent error when there is no corresponding language found
console.log(languageName);
EDIT:
With multiple user, it will be:
var languages = [
{"name": "english", "iso_639_2_code": "eng"},
{"name": "esperanto","iso_639_2_code": "epo"},
{"name": "estonian","iso_639_2_code": "est"}
];
var users = [
{name: "john",language: "eng",country: "US"},
{name: "john",language: "epo",country: "Esperanto"}
];
var languageNames = languages.filter(
l => users.find(u => l.iso_639_2_code === u.language)
).map(lang => lang.name);
console.log(languageNames);
Use find
var output = languages.find(s => s.iso_639_2_code == user[0].language).name;
Demo
var languages = [{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
];
var user = [{
name: "john",
language: "eng",
country: "US"
}
];
var output = languages.find(s => s.iso_639_2_code == user[0].language).name;
console.log(output);
Or, if there are multiple users, and you want to find language name for each of them, then use map
var output = user.map(t =>
languages.find(s =>
s.iso_639_2_code == t.language).name);
Demo
var languages = [{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
];
var user = [{
name: "john",
language: "eng",
country: "US"
}
];
var output = user.map(t =>
languages.find(s =>
s.iso_639_2_code == t.language).name);
console.log(output);
I think here is what you need , for output just run the snippet :
var languages = [
{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
];
var user = [
{
name: "john",
language: "eng",
country: "US"
}
];
user.map(u => {
let flang = languages.filter(lang => lang.iso_639_2_code === u.language);
if(flang) {
u.language = flang[0].name;
}
return u;
})
console.log(user);
var languages=[
{"name":"english","iso_639_2_code":"eng"},
{"name":"esperanto","iso_639_2_code":"epo"},
{"name":"estonian","iso_639_2_code":"est"}
];
var user=[
{name:"john",language:"eng",country:"US"}
];
var languageFound = languages.find(lang => lang.iso_639_2_code === user[0].language);
if(languageFound){
var languageName = languageFound.name;
console.log(languageName);
}

Pivot or Transforming JavaScript object

I have the following JavaScript object. I need to generate a new object from the given object. What is the approach I should take in JavaScript?
[
{"name": "Dan", "city" : "Columbus", "ZIP":"47201"},
{"name": "Jen", "city" : "Columbus", "ZIP":"47201"},
{"name": "Mark", "city" : "Tampa", "ZIP":"33602"},
]
How can I transform or pivot to generate the following object?
[
{ "47201": [
{"name": "Dan", "city": "Columbus"},
{"name": "Jen", "city": "Columbus"},
],
"count": "2"
},
{ "33602": [
{"name": "Mark", "city": "Tampa"}
],
"count": "1"
}
]
I don't know why you want the .count property, when that can be accessed via the array's .length property, but anyway:
const input = [
{"name": "Dan", "city" : "Columbus", "ZIP":"47201"},
{"name": "Jen", "city" : "Columbus", "ZIP":"47201"},
{"name": "Mark", "city" : "Tampa", "ZIP":"33602"},
]
const working = input.reduce((acc, {ZIP, name, city}) => {
(acc[ZIP] || (acc[ZIP] = [])).push({name, city})
return acc
}, {})
const output = Object.keys(working)
.map(k => ({[k]: working[k], count: working[k].length}))
console.log(output)
Further reading:
Array .reduce()
Array .map()
Object.keys()
Unpacking fields from objects passed as function parameters
Computed property names
The below code will work for your requirement. The final result is stored in the variable result which holds the array object.
var source = [{
"name": "Dan",
"city": "Columbus",
"ZIP": "47201"
},
{
"name": "Mark",
"city": "Tampa",
"ZIP": "33602"
},
{
"name": "Jen",
"city": "Columbus",
"ZIP": "47201"
}
];
var result = [];
finalarr('ZIP');
function finalarr(propname) {
var obj = JSON.parse(JSON.stringify(source));
obj.forEach(function(elm,i) {
var arr = {};var chli=[];var charr={};
var flag = 0;
for (var prop in elm) {
if(prop != propname){
charr[prop]=elm[prop];
}
}
for(var i=0;i<result.length;i++){
if(result[i][elm[propname]]){
result[0][elm[propname]].push(charr);
//console.log(result[i][propname]);
flag = 1;
}
}
if(flag == 0){
chli.push(charr);
arr["count"] = checkarr(obj,propname,elm[propname]);
arr[elm[propname]]=chli;
result.push(arr);
}
});
}
function checkarr(obj,propname,value){
var count = 0;
obj.forEach(function(elm,i) {
if(elm[propname] == value){
count++;
}
});
return count;
}
console.log(result);

Categories

Resources