How do I insert array value into object?

rsenecaljr

I want to create an object with nested arrays that will look something like this:

[{"name":"Joe","role":["Admin","Developer"]},{"name":"Sherry","role":["Analyst","QA-tester"]}]

I want to be able to do things like find the roles array for Sherry and add more values to that array. How can I go about this?

employees = [];

// [*query database for names and associated roles*]

employees.push({name: exampleVar1,role:exampleVar2});

Expected results: I want to store names that I can use to insert roles associated to the employee. Then later use this object as a reference.

trincot

You could use a function that will find the employee (or create it, if not found), add the role(s) and make sure a role will not be added, if already present:

function addRoles(employees, name, role) {
  let match = employees.find(e => e.name === name);
  if (match) {
    match.role = [...new Set(match.role.concat(role))];
  } else {
    match = { name, role };
  }
  return match;
}

let employees = [{"name":"Joe","role":["Admin","Developer"]},{"name":"Sherry","role":["Analyst","QA-tester"]}];

let result = addRoles(employees, "Sherry", ["Manager", "QA-tester"]);

console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i update value of a key in an object inside an Array in React?

How do I shift all values in an array to the right and insert a new first value?

how do I find the value stored in array of object and console the key?

How do I take all of an object's properties and insert them into its own object array property?

How do I get the value of a classed object from an array

How do I turn key value pairs in object into array of objects?

How can I insert value in a char array?

how do i update state in react for updating the value of an object inside an array using array.map function

how do I find an object value in a javascript array?

How do i insert a value taken from text box (object) on a form into a 'LIKE' Query in MS Access

How do I insert a multidimensional array into another array in a loop?

how do i insert a string into an array in c

How do I filter an object that is part of an array value?

How do I insert an array inside an empty array in PHP?

How do I modify an object value in a cloned array of objects in Javascript

How do I insert char* to array of structures?

Object property is null, but the value entered is not. How do i insert the value properly?

How do I insert meta data to my customers in woocommerce using an object array?

how to insert object key into object value and turn it into an array?

How to conditionally insert object to an array with the spread operator based on a string value

How do I convert Array of object key value to Object in object key value

How do i push same object value in to an array

How to insert value from an array to an array of object

How do I insert a new object into object and unwrap this new object?

How can I insert value in array inside object field in mongodb?

How do i filter the array in object of array?

How do I insert text into respective HTML buttons from the value of JavaScript object properties?

How to insert object of label and value inside array

How do I change the value of the array in the object in the array?