get the highest role from array - javascript

I currently have an array u._roles that is filled with id's from groups like "581199939464462340".
Secondly I have a dictionary where the key is a role id and the value is the name, like {key: "581199939464462340", value: "Member"}.
Now I want to get the name of the "highest" role of the array. Currently I have this ugly if statement but I think that can be done better, but how? (The array u._roles is read only).
u._roles could look like this:
["5812040340469414946", "581200340469415946", "581209222700597248"] so the "highest" would be "581200340469415946" (second if). (And then get the name of this id from the dictionary roleMap)
var Rank;
if (u._roles.includes("581199939464462340")) {
Rank = roleMap["581199939464462340"]; // highest
} else if (u._roles.includes("581200340469415946")) {
Rank = roleMap["581200340469415946"]; //
} else if (u._roles.includes("581214123334041620")) {
Rank = roleMap["581214123334041620"]; //
} else if (u._roles.includes("588976480017448988")) {
Rank = roleMap["588976480017448988"]; //
} else if (u._roles.includes("581203853635223574")) {
Rank = roleMap["581203853635223574"]; //
} else if (u._roles.includes("581209222700597248")) {
Rank = roleMap["581209222700597248"]; //
} else if (u._roles.includes("592436270031175681")) {
Rank = roleMap["592436270031175681"]; // lowest
} else {
Rank = "";
}
Highest don't mean the highest number. Its just an order I like to use.

I completely changed this answer with some new insight. Hopefully this is a bit more helpful.
const rolePriority = {a:1, b:2, c:3, d:4};
const u = {"_roles": ['b','c', 'a']};
const rankNumber = u._roles.reduce((highest, role) => {
const rank = rolePriority[role];
return rank < highest ? rank : highest;
}, Infinity);
const rank = rankNumber === Infinity ? "" : roleMap[rankNumber]

We just save the right order in an array, loop it and check whether the value is included in roles. If yes find will return it and we can put it into roleMap[findvalue].
let order = ["581199939464462340", "581200340469415946", "581214123334041620", "588976480017448988", "581203853635223574", "581209222700597248","592436270031175681"],
roles = ["5812040340469414946", "581200340469415946", "581209222700597248"];
let rank = order.find(v => roles.includes(v));
console.log(rank) // => 581200340469415946
Replace the rank line in your code with:
let rank = roleMap[order.find(v => roles.includes(v))]
If your order or amount of elements in orders is likely to change this will be messy, if not, you can just save them for comparison like this.

Firstly copy the array, then sort it by the role property and take the first element.
var data = [{
name: "Rank 1",
role: "5812040340469414946",
},
{
name: "Rank 2",
role: "581200340469415946"
},
{
name: "Rank 3",
role: "581209222700597248"
}
];
// Copy the source array to avoid mutating it using destructuring it
var copy = [...data];
// Sort by role property
var rank = copy.sort((a, b) => (+a.role) - (+b.role))[0].name;
console.log(rank);

Related

Get a key in a JavaScript object by its value and rename it

I have a list of variables that I will form into an array with keys and values. However, before making them as array I want to check if any of their values matches a
specific string and change their keys names into something else.
Lets say this is the list of variables
customer1 = "Jack", customerDate1 = "08/13/2021", customer2 = "Michael", customerDate2 = "01/01/2021", customer3 = "Luna",customerDate3 = "03/10/2021";
The array before running condition check will be
data = [{key1:"Jack", keyDate1:"08/13/2021",key2:"Michael", keyDate2:"01/01/2021",key3:"Luna", keyDate3:"10" }];
Lets say the condition is:
customerName = "Jack";
I want the cross check customerName variable with the following variables (customer1,customer2,customer3) and if the condition meets any of them, then their keys in the array changes:
for example the condition meets customer1 then both keys of customer1 and customerDate1 changes to something else, to become something like
data = [{conditionMet1:"Jack", conditionDateMet1:"08/13/2021",key2:"Michael", keyDate2:"01/01/2021",key3:"Luna", keyDate3:"10" }];
I am appreciating any help or guidance.
A little bit tricky as alghoritm but it works:
let data = [{key1:"Jhon", keyDate1:"08/13/2021",key2:"Eric", keyDate2:"01/01/2021",key3:"Jack", keyDate3:"10" }];
let i = 1;
let result = [];
let explored = [];
data.forEach(x => {
let resultObj = {};
for (const [key, val] of Object.entries(x)) {
let newKey = key;
let newKeyDate = null;
if (val === "Jack") {
newKey = "conditionMet" + i;
newKeyDate = "conditionDateMet" + i;
}
if (!explored.includes(key)) resultObj[newKey] = val;
if (newKeyDate) {
resultObj[newKeyDate] = x["keyDate" + i];
explored.push("keyDate" + i)
}
if(!key.includes("Date")) i++;
}
result.push(resultObj)
})
console.log(result)
Basically for each element in data I explore all the entries and if I found condition I add conditionMet1 and conditionDateMet1 to object, otherwise what I found on object itself.
Perhaps not very elegant, but for the example given you could do something like this:
if(specialCondition){
data = [{specialKey1:customer1, specialDate1:customerDate1,key2:customer2...];
}else{
data = [{key1:customer1, keyDate1:customerDate1,key2:customer2...];
}
I don't think it is possible to rename a key but you can always create a new key by
object["newKeyName"] = object.previousKey
And optionally you can remove the old key by
delete object.previousKey
That way you will remove the previous key if you don't want it anymore.
Say you have 6 variables.
customer1, customerDate1, customer2, customerDate2, customer3,customerDate3;
You could make an array, by customer
const data = [{
id: customer1,
date: customerDate1
},{
id: customer2,
date: customerDate2
},{
id: customer3,
date: customerDate3
}];
This is now quite useful. Say you want to know which customer meets some condition. (for example id is "Jack"), using Array.find
const jack = data.find(item => item.id === "Jack");
console.log(jack);
Or to find which customers have a date before "now", using Array.filter
const now = Date.now();
const beforeNow = data.filter(item => item.date < now);
console.log(beforeNow);

how to get the deleted item value using javascript

I have removed an element from an array. Now, I need to display which item is deleted and the remaining items, but have been unsuccessful. Suppose, the array looks like:
let person = ['Cercei','Snow','Arya','Sansa']
I have removed 'Snow' and 'Cercei'. So, I need to display two things into two different new array.
delPerson = ['Cercei','Snow']
remArray = ['Arya','Sansa']
How can this be done with JavaScript? In my application, I have delete icon with each and every name so i can remove any name. I want to get the deleted name and remaining name into two different array. Please help me how to do that using javascript or lodash
let me tell you my application code:
let person = personData.map(n => n.name)
let remPerson = person.filter(p => person.indexOf(p) !== -1)
Now, person and remPerson array is showing same value. While comparing the length it is creating the problem.
One way could be to use Array.reduce.
const persons = ['Cercei', 'Snow', 'Arya', 'Sansa'];
const {
deleted,
kept
} = persons.reduce((acc, person) => {
// Note: The deletion condition may vary
if (person === 'Cercei' || person === 'Snow') {
acc.deleted.push(person);
} else {
acc.kept.push(person);
}
return acc;
}, {
deleted: [],
kept: []
});
console.log(deleted);
console.log(kept);
You can use splice, but this will modify your original array. It is unclear to me if that will be ok in the OP's case. Here's Mozilla's documentation on splice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
let person = ['Cercei','Snow','Arya','Sansa'];
let delPerson = person.splice(0, 2);
console.log(delPerson);
console.log(person);
You can remove items using splice and also keep track of two separate arrays:
let personData = [{name:'Cercei'},{name:'Snow'},{name:'Arya'},{name:'Sansa'}];
let person = personData.map(n=>n.name);
let removeItems = ['Cercei','Snow'];
let delPerson = [];
removeItems.forEach(item=>{
delPerson.push(
...person.splice( person.indexOf(item), 1 )
)
});
console.log('delPerson: ', delPerson);
console.log('remaining: ', person);

Updating javascript string to an array

I got an URL containing a bunch of parameters, hereunder the following as well;
conditions=[{"name”:”foo”,”operator”:”bar”,”value":350000}]
This can also be the folowing
conditions=[]
and
conditions=[{"name”:”one”,”operator”:”two”,”value":350000}, {"name”:”three”,”operator”:”one”,”value”:22}]
It could theoretically also be
conditions=[{"name”:”one”,”operator”:”two”,”value":350000}, {"name”:”three”,”operator”:”one”,”value”:22}, {"name”:”four”,”operator”:”two”,”value”:22}, {"name”:”sixty”,”operator”:”three”,”value”:22}, ..]
Based on an input I want to change the conditions=[]. I got the following variables in my code
inputName, obj, valueOperator
These could contain the following values:
inputName = “three”
valueOperator = “two”
obj.value = 22
So from this i can construct the conditions as describer earlier. Now to my issue;
I want to break up the conditions=[] and check if there is anything in the brackets, if not, I’ll just add my new condition. If there is something, I want to check if the inputName matches any “name” in bracket. If not, I’ll just add my new condition. If it matches I want to update the value and operator with my new variables. When I’m done I want to put back together the conditions=[]. With the updated conditions. It will always only update one condition at a time.
I'm pretty much lost. Does anybody have a great idea?
From the explanation you have provided, i tried to replicate it. Hope this works.
const inputName = 'three';
const valueOperator = 'two';
const obj = 22;
let conditions = [
{ name: 'one', operator: 'two', value: 350000 },
{ name: 'two', operator: 'one', value: 22 }
];
if (conditions.length == 0) {
conditions.push({ name: inputName, operator: valueOperator, value: obj });
} else {
let found = false;
for (const condition of conditions) {
if (condition.name == inputName) {
condition.operator = valueOperator;
condition.value = obj;
found = true; // mark found as true when name found
}
}
// Insert values if not found in conditions
if (!found) {
conditions.push({ name: inputName, operator: valueOperator, value: obj });
}
}
console.log(conditions);
If you have the following url:
https://example.com/myfolder/?conditions=[{"name”:”foo”,”operator”:”bar”,”value":350000}]
then the value of the queryString parameter conditions is a JSON string.
To turn that value into a javascript object, you need the following:
// CAPTURE THE QUERY STRING IN A URLSearchParams OBJECT INSTANCE
let urlParams = new URLSearchParams(window.location.search);
// GET THE RELEVANT JSON STRING FROM urlParams
let conditionsJSON = urlParams.get('conditions');
// CONVERT THE JSON STRING INTO A JAVASCRIPT OBJECT
let conditionsObject = JSON.parse(conditionsJSON);
You will now have an array (conditionsObject) which looks like this:
[
{
name : "foo",
operator : "bar",
value : 350000
}
]
N.B. To avoid confusion, in javascript, an array is a specific kind of object - so the code above represents both an array and an object (because the former represents a subset of the latter).
This code should work
const inputName = 'foo'
const valueOperator = 'two'
const value = 22
const conditionString = 'condition=[{"name”:”foo”,”operator”:”bar”,”value":350000}]'
const data = JSON.parse(conditionString.substr(10).replace(/”/g, '"'))
const newData = { name: inputName, operator: valueOperator, value: value }
let ind = data.findIndex(v => v.name === inputName)
if (ind < 0) ind = data.length
data[ind] = newData
const newConditionsString = `conditions=${JSON.stringify(data)}`
console.log(newConditionsString)
First part of your problem can be taken care by the solution provided by Rounin. For the second part where you want to work on condition matching based on name, below code should be able to help you.
inputName="Three"
valueOperator = "two"
value = 22
if(conditionsObject.length!=0){
let index = conditionsObject.findIndex((item)=>item.name==inputName)
if(index!=-1){
conditionsObject[index].value = value
conditionsObject[index].operator = valueOperator
}
else{
conditionsObject.push({ name: inputName, operator: valueOperator, value: value })
}
else{
conditionsObject.push({ name: inputName, operator: valueOperator, value: value })
}
}

optimal algorithm grouping data in javascript

The following (simplified) json type of data defines a Contact:
{
id: number;
name: string;
phone: string;
email: string
}
There is the following group of data:
+---+----------+-------------+---------------------------+
|id | name | phone |email |
+---+----------+-------------+---------------------------+
|1 | John | 11111111 |aaaa#test.com |
|2 | Marc | 22222222 |bbbb#test.com |
|3 | Ron | 99999999 |aaaa#test.com |
|4 | Andrew | 55555555 |dddd#test.com |
|5 | Wim | 99999999 |gggg#test.com |
|6 | Marc | 33333333 |cccc#test.com |
|7 | Dan | 44444444 |cccc#test.com |
+---+----------+-------------+---------------------------+
Goal is to find the groups that belong together using javascript(optionally in lodash, but main idea is to get the algorithm clear) according to the following constraint: a contact belongs to a group when any of the following criteria are the same: name, phone or email. The results shows the id's grouped as arrays in arrays. a contact in a group of 1 is ignored.
In the example above, it means that contacts with ids 1,3,5 belong together since 1,3 share the same email and 3 and 5 share the same phonenumber. Likewise 2,6,7: 2 and 6 have the same name and 6 and 7 have the same email. 5 does not have anything in common.
The expected result therefore is:
[[1,3,5], [2,6,7]]
Background:
One solution that works is iterating over every item and check for the remainder of the list if name, email, or phone is the same. If so, group them and take them out of the list (in the example we compare 1 with all the items in the list and only 3 is found). problem is that next items also need to be checked again to these groups, because in this case 5 is not yet detected as part of the group. This makes the algorithm complex, while I suspect there is a simple way of solving this in linear time. There might also be a name for this class of problems?
`
Idea:
Start with 0 groups
Iterate your list of contacts
Check if there is a group with the contact name, or phone, or email. Merge all the members of those groups as the same group. Then add yourself to that group. If not, begin a new group with yourself and set the name, phone and email group as yourself.
Union find is an efficient structure to handle merging of disjoint sets. Code taken from here. As it uses path compression and union by rank, you can consider that the whole code is linear in the amount of contacts.
var data = [
{id:1,name:'John',phone:'11111111',email:'aaaa#test.com'},
{id:2,name:'Marc',phone:'99999999',email:'bbbb#test.com'},
{id:3,name:'Ron',phone:'99999999',email:'aaaa#test.com'},
{id:4,name:'Andrew',phone:'55555555',email:'dddd#test.com'},
{id:5,name:'Wim',phone:'99999999',email:'gggg#test.com'},
{id:6,name:'Marc',phone:'33333333',email:'cccc#test.com'},
{id:7,name:'Dan',phone:'44444444',email:'cccc#test.com'}
];
// UNION-FIND structure, with path comression and union by rank
var UNIONFIND = (function () {
function _find(n)
{
if(n.parent == n) return n;
n.parent = _find(n.parent);
return n.parent;
}
return {
makeset:function(id){
var newnode = {
parent: null,
id: id,
rank: 0
};
newnode.parent = newnode;
return newnode;
},
find: _find,
combine: function(n1, n2) {
var n1 = _find(n1);
var n2 = _find(n2);
if (n1 == n2) return;
if(n1.rank < n2.rank)
{
n2.parent = n2;
return n2;
}
else if(n2.rank < n1.rank)
{
n2.parent = n1;
return n1;
}
else
{
n2.parent = n1;
n1.rank += 1;
return n1;
}
}
};
})();
var groupHash = {name: {}, phone: {}, email: {}}
var groupNodes = []
data.forEach(function(contact){
var group = UNIONFIND.makeset(contact.id);
var groups = new Set();
["name", "phone", "email"].forEach(function(attr){
if (groupHash[attr].hasOwnProperty(contact[attr])) groups.add(groupHash[attr][contact[attr]])
});
groups = Array.from(groups);
groups.push(group);
groupNodes.push(group);
for(var i = 1; i < groups.length; i++) {
UNIONFIND.combine(groups[0], groups[i]);
}
["name", "phone", "email"].forEach(function(attr){
groupHash[attr][contact[attr]] = groups[0];
});
})
var contactsInGroup = {}
groupNodes.forEach(function(group){
var groupId = UNIONFIND.find(group).id;
if (contactsInGroup.hasOwnProperty(groupId) == false) {
contactsInGroup[groupId] = [];
}
contactsInGroup[groupId].push(group.id);
})
var result = Object.values(contactsInGroup).filter(function(list){
return list.length > 1
})
console.log(result)
Any answer that iterates over each of n entries, and then over a growing list of m groups to match against is going to have worst-time performance of O(n*m) (found when there are no two entries that match on any term).
Any answer that iterates over each entry, and then over groups, and uses arrays to test for matching values among q options will further have to pay a penalty of O(q) per match. In the worst case, with say all e-mails the same and all phones different, this will mean O(n*m).
I believe this answer is O(n), because assuming that the number of fields to match with is a constant (in this case, 3: name, phone and email), all operations in the main loop, which is run once per entry, are O(1).
There is an extra complication to fix the fact that, late in the process, we may find a bridge between two (or even 3) groups, as entries can match on different fields with entries from different groups. This may happen several times. To avoid having to rebuild groups during the main loop, we leave merging to the very end, where we first build a map of what-group-ends-up-where, and then finally move all entry IDs to their final group. This can all be done in O(m), with m the number of groups; with an extra O(n) when actually copying entry IDs to the merged groups: overall, we are still in O(n) territory.
The last line builds arrays of ids from the merged groups, and filters out any that does not have over 1 element.
const data = [
{id:1,name:'John',phone:'11111111',email:'aaaa#test.com'},
{id:2,name:'Marc',phone:'99999999',email:'bbbb#test.com'},
{id:3,name:'Ron',phone:'99999999',email:'aaaa#test.com'},
{id:4,name:'Andrew',phone:'55555555',email:'dddd#test.com'},
{id:5,name:'Wim',phone:'99999999',email:'gggg#test.com'},
{id:6,name:'Marc',phone:'33333333',email:'cccc#test.com'},
{id:7,name:'Dan',phone:'44444444',email:'cccc#test.com'}
];
const groups = function(inputs) {
let valuesToGroups = new Map(
['name', 'phone', 'email'].map(key => [key, new Map()]));
let groups = new Map();
let pendingMerges = [];
for (const entry of inputs) {
let group = undefined;
let found = [];
for (const [key, valueMap] of valuesToGroups) {
// look up value in values-index for current key
group = valueMap.get(entry[key]);
if (group !== undefined) {
found.push(group);
// not breaking allows groups to be merged
}
}
if (found.length === 0) {
// not found: create new group
group = groups.size;
groups.set(group, [entry.id]);
} else {
// found: add entry to chosen group
group = found[0];
groups.get(group).push(entry.id);
if (found.length > 1) {
pendingMerges.push(found);
}
}
// add entry's values to index, pointing to group
for (const [key, valueMap] of valuesToGroups) {
valueMap.set(entry[key], group);
}
}
// do pending merges; initially, all groups are stand-alone
let merges = new Map(Array.from(groups.keys()).map(k => [k, k]));
for (const merge of pendingMerges) {
// contents will go to the lowest-numbered group
const sorted = merge.map(groupId => merges.get(groupId)).sort();
sorted.forEach(groupId => merges.set(groupId, sorted[0]));
}
const cleanGroups = new Map();
groups.forEach((value, key) => {
const k = merges.get(key);
if ( ! cleanGroups.has(k)) {
cleanGroups.set(k, []);
}
value.forEach(id => cleanGroups.get(k).push(id))
})
// return only non-empty groups
return [... cleanGroups].filter(g => g[1].length>1).map(g => [... g[1]]);
}(data);
console.log(""+JSON.stringify(groups))
// output is [[1,2,3,5,6,7]]
Here is another suggestion of a route you could take. The idea is to use one Array.reduce to group by id and keep all the values (vls) and combined results (ids) in that accumulator object.
This way you can easily compare the name/phone/email using Array.some + Array.includes (which is what the getGroupId function does).
Once you have grouped and have the almost final result just prettify it by removing the groups with length of one and picking only the ids array of the rest:
var data = [ {id:1,name:'John',phone:'11111111',email:'aaaa#test.com'}, {id:2,name:'Marc',phone:'22222222',email:'bbbb#test.com'}, {id:3,name:'Ron',phone:'99999999',email:'aaaa#test.com'}, {id:4,name:'Andrew',phone:'55555555',email:'dddd#test.com'}, {id:5,name:'Wim',phone:'99999999',email:'gggg#test.com'}, {id:6,name:'Marc',phone:'33333333',email:'cccc#test.com'}, {id:7,name:'Dan',phone:'44444444',email:'cccc#test.com'} ];
const getGroupId = (obj, vals) => Object.entries(obj)
.find(([k,v]) => v.vls.some(x => vals.includes(x))) || []
const group = d => d.reduce((r, c) => {
let values = Object.values(c), groupID = getGroupId(r, values)[0]
if(!groupID)
r[c.id] = ({ vls: values, ids: [...r[c.id] || [], c.id] })
else {
r[groupID] = ({
vls: [...r[groupID].vls, ...values], ids: [...r[groupID].ids, c.id]
})
}
return r
}, {})
const prettify = grp => Object.values(grp).reduce((r,c) => {
if(c.ids.length > 1)
r.push(c.ids)
return r
}, [])
console.log(prettify(group(data)))
One thing to note is that we do not care about the number of properties since we do Object.values. So you can easily add another address or fax to that list and it would still work with zero code changes.
As per feedback here is another version which works slightly different:
var data = [ {id:1,name:'John',phone:'11111111',email:'aaaa#test.com'}, {id:2,name:'Marc',phone:'22222222',email:'bbbb#test.com'}, {id:3,name:'Ron',phone:'99999999',email:'aaaa#test.com'}, {id:4,name:'Andrew',phone:'55555555',email:'dddd#test.com'}, {id:5,name:'Wim',phone:'99999999',email:'gggg#test.com'}, {id:6,name:'Marc',phone:'33333333',email:'cccc#test.com'}, {id:7,name:'Dan',phone:'44444444',email:'cccc#test.com'} ];
var testData = [{ id: 1, name: 'John', phone: '1', email: 'a' }, { id: 2, name: 'Marc', phone: '2', email: 'b' }, { id: 3, name: 'Ron', phone: '1', email: 'b' }];
const getGroupId = (obj, vals) => Object.entries(obj)
.find(([k,v]) => v.vls.some(x => vals.includes(x))) || []
const group = d => d.reduce((r,c,i,a) => {
let values = Object.values(c), groupID = !i ? i : getGroupId(r, values)[0]
if (!groupID) {
let hits = a.filter(x =>
x.id != c.id && values.some(v => Object.values(x).includes(v)))
hits.forEach(h =>
r[c.id] = ({ vls: [...values, ...Object.values(h)], ids: [c.id, h.id] }))
}
else
r[groupID] = r[groupID].ids.includes(c.id) ? r[groupID] :
({ vls: [...r[groupID].vls, ...values], ids: [...r[groupID].ids, c.id] })
return r
}, {})
const prettify = grp => Object.values(grp).reduce((r, c) => {
if (c.ids.length > 1)
r.push(c.ids)
return r
}, [])
console.log(prettify(group(data))) // OP data
console.log(prettify(group(testData))) // Test data
The reason for this version is due to the testData provided by #Mark which has the 2nd element not matching the first but matching the 3rd which actually matches the 1st ... so they all should be hits.
To get to that once we find a match we look for matches of that same initial match and push in the same group so we can have the maximum amount of data to match on.
The result is that once we get the first group with the first element we then find and push the 3rd as well and from there it is much easier to match the 2nd. The logic is slightly more complex and I would imagine less performant.
One way to accomplish what you need, is to separate the contacts into groups.
Each group will contain a list of names, phones and emails.
Then iterate through contacts, and see if the current contact falls into any of the groups. If not, create a new group and set its names/phones/emails so that next contacts might fall into the same on.
var data = [
{id:1,name:'John',phone:'11111111',email:'aaaa#test.com'},
{id:2,name:'Marc',phone:'22222222',email:'bbbb#test.com'},
{id:3,name:'Ron',phone:'99999999',email:'aaaa#test.com'},
{id:4,name:'Andrew',phone:'55555555',email:'dddd#test.com'},
{id:5,name:'Wim',phone:'99999999',email:'gggg#test.com'},
{id:6,name:'Marc',phone:'33333333',email:'cccc#test.com'},
{id:7,name:'Dan',phone:'44444444',email:'cccc#test.com'}
];
var groups = [];
data.forEach(function(person){
var phone = person.phone;
var email = person.email;
var name = person.name;
var id = person.id;
var found = false;
groups.forEach(function(g){
if( g.names.indexOf(name) > -1
|| g.phones.indexOf(phone)>-1
|| g.emails.indexOf(email)>-1) {
found = true;
g.names.push(name);
g.phones.push(phone);
g.emails.push(email);
g.people.push(id);
}
});
if(!found) {
groups.push({names:[name],phones:[phone],emails:[email],people:[id]});
}
});
var output=[];
groups.forEach(function(g){
output.push(g.people);
});
console.log(output); //[ [1,3,5] , [2,6,7] , [4] ]

storing key value pairs in an array in javascript

I have 2 arrays namely,
configdata = ["assignee", "shortDesc"];
ticketarray = ["Tom","testDesc"];
I want to store the values as a key value pair in another array, something like this:
ticketData = ["assignee":"Tom","shortDesc":"testDesc"];
Kindly note that the array values are dynamic, so I cannot hardcode them.
Is there a way to do so? I am able to achieve the above said requirement but the length always shows 0. This is the code that I am using:
configdata.Incident_Field.forEach(function (k, i) {
this[k] = ticketarray[i];
}, ticketData);
Other people have explained why your code did not work. I am providing another solution using reduce.
const configdata = ["assignee", "shortDesc"];
const ticketarray = ["Tom", "testDesc"];
let ticketData = configdata.reduce((result, value, index) => {
result[value] = ticketarray[index];
return result;
}, {});
console.log(ticketData);
Output:
{
assignee: "Tom",
shortDesc: "testDesc"
}
The below is not a valid structure in JavaScript:
ticketData = ["assignee":"Tom","shortDesc":"testDesc"];
What you need is a JavaScript object. The best you can do is:
Make sure both the array lengths are same.
Associate the key and value by creating a new object.
Use Object.keys(obj).length to determine the length.
Start with the following code:
configdata = ["assignee", "shortDesc"];
ticketarray = ["Tom", "testDesc"];
if (configdata.length == ticketarray.length) {
var obj = {};
for (var i = 0; i < configdata.length; i++)
obj[configdata[i]] = ticketarray[i];
}
console.log("Final Object");
console.log(obj);
console.log("Object's Length: " + Object.keys(obj).length);
The above will give you an object of what you liked, a single variable with all the values:
{
"assignee": "Tom",
"shortDesc": "testDesc"
}

Categories

Resources