0

I have a code in JavaScript and i would like to append "i" to each item of the array inside the object. here is the code. Could anyone go through it and fix this code

    const forArray = [
    {
      username: "john",
      team: "red",
      score: 5,
      items: ["ball", "book", "pen"]
    },
    {
      username: "becky",
      team: "blue",
      score: 10,
      items: ["tape", "backpack", "pen"]
    },
    {
      username: "susy",
      team: "red",
      score: 55,
      items: ["ball", "eraser", "pen"]
    },
    {
      username: "tyson",
      team: "green",
      score: 1,
      items: ["book", "pen"]
    },
  
  ];

    const itemsArray = [];
    forArray.forEach(item => {
      let{items} = item;
      items = items + "i";
      itemsArray.push(items);
  })
  console.log(itemsArray);

3 Answers 3

1

Using map()

const forArray = [{username:"john",team:"red",score:5,items:["ball","book","pen"]},{username:"becky",team:"blue",score:10,items:["tape","backpack","pen"]},{username:"susy",team:"red",score:55,items:["ball","eraser","pen"]},{username:"tyson",team:"green",score:1,items:["book","pen"]}];

const itemsArray = forArray.map(profile => 
  profile.items.map(item => item + 'i').join(',')
)
console.log(itemsArray);

Sign up to request clarification or add additional context in comments.

2 Comments

t does the job but i am looking to print out the result Array(4) 0: "balli ,booki ,peni" 1: "tapei ,backpacki ,peni" 2: "balli ,eraseri ,peni" 3: "booki ,peni" length: 4
can each group of items be on the same line ?
0

You could use Map and append i to each element in the array and then use flat()to flatten the arrays

const forArray = [ { username: "john", team: "red", score: 5, items: ["ball", "book", "pen"] }, { username: "becky", team: "blue", score: 10, items: ["tape", "backpack", "pen"] }, { username: "susy", team: "red", score: 55, items: ["ball", "eraser", "pen"] }, { username: "tyson", team: "green", score: 1, items: ["book", "pen"] }, ];

    const itemsArray = [];
    forArray.forEach(item => {
      let{items} = item;
      itemsArray.push(items.map(o=>o+"i"))
  })
  console.log(itemsArray);

1 Comment

it does the job but i am looking to print out the result Array(4) 0: "balli ,booki ,peni" 1: "tapei ,backpacki ,peni" 2: "balli ,eraseri ,peni" 3: "booki ,peni" length: 4
0

Is this what you want? The code below will keep your original structure intact but append an i to the end of each element within the items array nested inside each object.

const forArray = [
    {
      username: "john",
      team: "red",
      score: 5,
      items: ["ball", "book", "pen"]
    },
    {
      username: "becky",
      team: "blue",
      score: 10,
      items: ["tape", "backpack", "pen"]
    },
    {
      username: "susy",
      team: "red",
      score: 55,
      items: ["ball", "eraser", "pen"]
    },
    {
      username: "tyson",
      team: "green",
      score: 1,
      items: ["book", "pen"]
    },
  
  ];

  forArray.map(item => item.items = item.items.map(i => i += 'i'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.