score:2
use #array.foreach and for each object of array add extra key with the value from swords array.
working snippet (this way, it will do the changes directly in the original array):
let chachters = [
{name: "frodo", race: "hobitt", age: 111},
{name: "gandalf", race: "human", age: 2019},
{name: "aragorn", race: "elf", age: 40}];
let swords = ["sting","glamdring","anduril"];
chachters.foreach((el,i) => {
el.sword = swords[i];
})
console.log('chachters = ', chachters);
if chachters
is a state array and you are updating the state then use this way:
let chachters = [
{name: "frodo", race: "hobitt", age: 111},
{name: "gandalf", race: "human", age: 2019},
{name: "aragorn", race: "elf", age: 40}];
let swords = ["sting","glamdring","anduril"];
let newchachters = chachters.map((el,i) => ({...el, sword: swords[i]}))
console.log('chachters = ', chachters);
console.log('newchachters = ', newchachters);
score:1
you can create a function to append the array of strings to the array of objects;
for example:
this function will be used to append the array of strings to the array of object
function appendobjto(swords, chachters ) {
return object.freeze(swords.concat(chachters ));
}
from what you defined:
swords = ["sting","glamdring","anduril"];
const chachters = [{name: "frodo", race: "hobitt", age: 111},
{name: "gandalf", race: "human", age: 2019},
{name: "aragorn", race: "elf", age: 40}];
const newchachters = appendobjto(swords, chachters);
score:1
allow me to try. i'm not so familiar with .map() :p
var characters = [
{name: "frodo", race: "hobitt", age: 111},
{name: "gandalf", race: "human", age: 2019},
{name: "aragorn", race: "elf", age: 40}
];
var swords = ["sting", "glamdring", "anduril"];
var characterswithswords = characters.map(function (character, index) {
character.swords = swords[index];
return character;
});
console.log(characterswithswords);
result:
> array [object { name: "frodo", race: "hobitt", age: 111, swords: "sting" }, object { name: "gandalf", race: "human", age: 2019, swords: "glamdring" }, object { name: "aragorn", race: "elf", age: 40, swords: "anduril" }]
score:3
you can use map
and object.assign
:
var chachters = [{name: "frodo", race: "hobitt", age: 111},
{name: "gandalf", race: "human", age: 2019},
{name: "aragorn", race: "elf", age: 40}],
swords = ["sting","glamdring","anduril"];
var result = chachters.map( (obj, i) => object.assign({ sword: swords[i] }, obj) );
console.log(result);
score:3
you can use array#map
with spread syntax
. add a sword to a character based on the index.
const chachters = [{name: "frodo", race: "hobitt", age: 111}, {name: "gandalf", race: "human", age: 2019}, {name: "aragorn", race: "elf", age: 40}],
swords = ["sting","glamdring","anduril"],
result = chachters.map((o,i) => ({...o, sword: swords[i]}));
console.log(result);
Source: stackoverflow.com
Related Query
- Adding key value pair (value from an array) to specific objects in an array
- Adding key value pair to specific objects in an array
- check if an object with a specific key value exists in an array of objects
- Array of objects and i want to return true if any key value pair in the object is duplicate
- Display key value pairs from looping through an array of objects
- How to update a single key value pair in an array of objects in react?
- Creating An Array of Objects From An Object Without Specific Key Values
- How to group arrays of objects by value and add the result to another array with same value at specific key
- How to get value based on key from an array of objects javascript
- best way to change value of an object from an array of objects when given a key
- How do I iterate over an array of objects match a common element from another array and return the key value for "name"
- Adding value to a props of an object from an array of objects
- React.js - How to get access to a specific value from an array of objects
- How can I load a key value pair array into state, by pulling data from a json file?
- How do I take an array of objects from state and push a specific value of the objects into a new array
- How to get value based on key from an array of objects in React
- get key values pair from an array of objects and place in new array
- How to update the value into a nested array of objects if value for specific key is same?
- Get specific value from array of objects [{"key":"value"}, {"key":"value"}]
- How to do a foreach for an array with values that have to match specific key and the same value from another array
- How to create new array of objects with globally replaced key value from a source array of objects
- Addup specific value from array of objects
- Populate react select options array with key value pairs from firebase collection
- Change value of a key in array of objects in redux reducer
- Get the max value for a date from an array of objects
- How to update specific value when the state is an array of objects - React
- Javascript 2 arrays of objects , get a value from one array of objects and assign it to the other array of objects
- Fetching the input value from textarea in a loop from an array of objects using reactjs. How to do so?
- Javascript merging objects within an array based on specific value
- how to filter object from nested objects by specific unique key
More Query from same tag
- Firestore doc field update in react app giving TypeError unless I refresh first
- Fetch data with React
- Access value outside Map Function in JSX
- problem with react slick: import { baseUrl } from "./config
- Pass url parameter from Appsettings.json (.core) to React
- React i18next::backendConnector issue whenever I change the namespace
- Why React app cannot connect to Local mongodb using Localhost while success in 127.0.0.1
- Updating state inside nested array when mapping the parent
- redux saga knowing when task is done
- ReactJS need to conditionally display different onclick vs Link based on logic
- Toggle language using React without Redux
- React changing body class before component is mounted
- Async/Await in useEffect(): how to get useState() value?
- Why local variables in React functional component?
- Formik Initial Values out of Sync with Redux
- How to add to state array in React
- React: TypeError: Cannot read property 'innerHTML' of null
- Why is my React component render called twice, once without data and then later with data, but too late exception?
- handling state during onChange event
- Is it a correct way to extend Material-UI components (React / Typescript)
- How to use backgroundImage css in styled component having typescript
- Error: Element type is invalid: expected a string in react ,when trying to update state
- Does React not require an import statement in APP.js anymore?
- Using the Django authentication system within a React SPA
- How to link to section in react toggle menu
- How to update state using Redux?
- Sitemap is not a constructor while using react-router-sitemap
- Redux reducer to remove an object from a list by name
- How do I get item from localstorage?
- Ant design sort table code not working on the react typescript