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 }]
VLAZ
  • 26,331
  • 9
  • 49
  • 67
its me
  • 524
  • 6
  • 28
  • 62
  • `allUser` has a *single* item. Getting `allUser[i]` is using the indexes from `users`, so with `i = 1`, `allUser[1]` produces `undefined`. – VLAZ Jan 29 '21 at 17:57
  • how can I get all users like this [{ "username":"John", "userage":30 },{ "username":"Raj", "userage":28 }] – its me Jan 29 '21 at 18:00
  • Option 1 - remove `` Option 2 [From an array of objects, extract value of a property as array](https://stackoverflow.com/q/19590865) + [How to get a subset of a javascript object's properties](https://stackoverflow.com/q/17781472) – VLAZ Jan 29 '21 at 18:04
  • what is the problem with option 2? any idea? – its me Jan 29 '21 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. – VLAZ Jan 29 '21 at 18:16
  • Please find my answer .i have just edited your code.map should have been a better option.But I have edited your code to get output. – CodeOfLife Jan 29 '21 at 18:21
  • @itsme also solved the problem with map function.Please have a look. – CodeOfLife Jan 29 '21 at 18:37

3 Answers3

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);
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
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.

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.

CodeOfLife
  • 295
  • 2
  • 11