0

Good morning guys

I have to create a new JSON object array using the user array but when I do this getting the below error. could someone help me here? or help me to understand the mistake?

var users = [{ "name":"John", "age":30, "car":"fiat" },{ "name":"Raj", "age":28, "car":"hundai" }];

var allUser =[]

for (var i = 0; i < users.length; i++) {
  test(i, users[i])
}
console.log(allUser)

function test(i, user) {
  console.log(i)
  <!-- allUser.push({"username":user.name}); -->

  allUser[i].username = user.name;
  //allUser[i].userage = user.age;
  //allUser[i].usercar = user.car;
}

enter image description here

Expected Result:

all user should be like this

[{ "username":"John", "userage":30 },{ "username":"Raj", "userage":28 }]
7
  • allUser has a single item. Getting allUser[i] is using the indexes from users, so with i = 1, allUser[1] produces undefined. Commented Jan 29, 2021 at 17:57
  • how can I get all users like this [{ "username":"John", "userage":30 },{ "username":"Raj", "userage":28 }] Commented Jan 29, 2021 at 18:00
  • Option 1 - remove <!-- --> Option 2 From an array of objects, extract value of a property as array + How to get a subset of a javascript object's properties Commented Jan 29, 2021 at 18:04
  • what is the problem with option 2? any idea? Commented Jan 29, 2021 at 18:09
  • There is no problem. Either uncomment the line that will add items to allUsers or use .map. I've pointed you to two resources for how to use .map to get a subsection of the keys of an object. Commented Jan 29, 2021 at 18:16

3 Answers 3

3

Your test function can be fixed in the following manner:

var users = [
  { "name": "John", "age": 30, "car": "fiat"   },
  { "name": "Raj",  "age": 28, "car": "hundai" },
];

var allUser = [];

for (var i = 0; i < users.length; i++) {
  test(i, users[i]);
}
console.log(allUser);

function test(i, user) {
  allUser[i] = {}; // create and assign a new object to `allUser[i]`
  allUser[i].username = user.name;
  allUser[i].userage = user.age;
  allUser[i].usercar = user.car;
}

However the above looks over complicated, and a map call would simplify things a lot.

var users = [
  { "name": "John", "age": 30, "car": "fiat"   },
  { "name": "Raj",  "age": 28, "car": "hundai" },
];

var allUser = users.map((user) => ({
  username: user.name,
  userage: user.age,
  usercar: user.car,
}));

console.log(allUser);

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

Comments

2

Please check the fiddle for solution. https://jsfiddle.net/gczwk2jf/ Your alluser array is empty array, when you try to get value by a[i] e.g. a[0] or a[1], it is undefined. You are trying to access username of undefined value hence you have an error. Best way is to map or compose your complete object and assign it your array index.

allUser[i] = {
username: user.name,
userage: user.age
}

With map, you can replace the entire code with following couple of lines

var users = [{ "name":"John", "age":30, "car":"fiat" },{ "name":"Raj", "age":28, "car":"hundai" }];
var allUser = users.map(user => return {
username: user.name,
userage: user.age
});

Map is a higher order function that doesn't mutate your original array, which means you get a new array after map, keeping original user array unchanged.

Comments

1

var users = [{ "name":"John", "age":30, "car":"fiat" },{ "name":"Raj", "age":28, "car":"hundai" }];

var allUser =[]

for (var i = 0; i < users.length; i++) {
  test(i, users[i])
}
console.log(allUser)

function test(i, user) {
var data={}

  data["username"]=user["name"];
  data["userage"]=user["age"]
  allUser.push(data);

}

using map function

var users = [{ "name":"John", "age":30, "car":"fiat" },{ "name":"Raj", "age":28, "car":"hundai" }];

var allUser =[]

users.map((element)=>{
test(element)
})
console.log(allUser)

function test(user) {
var data={}

  data["username"]=user["name"];
  data["userage"]=user["age"]
  allUser.push(data);

}

Please try this.

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.