1

I am trying to add more things inside an object but I am not sure how to do it

I have tried pushing the data into the object but it doesn't work

people = [{
  name: 'robert',
  year: 1993
}];

//this is what i want to add

people = {
  name: 'joe',
  year: 1992
}

//I want it to act as if it was

people = [{
  name: 'robert',
  year: 1993
}, {
  name: 'joe',
  year: 1992
}];

I want to be able to call it back using people[0].name

2
  • It's typescript, call the Person out for what they are. people as {}[] and people as {} is confusing. Singular of people is Person so try Person[] where Person is a well-defined type. Commented Jul 3, 2019 at 5:19
  • "How can I add more data to an json based object in Typescript?" -- by extension or composition. Commented Jul 3, 2019 at 5:20

2 Answers 2

3

Assuming you want the people variable to be an array of objects,

const people = [{ name: 'robert', year: 1993 }];

If you want to add an object to people,

const newPerson = { name: 'joe', year: 1992 };
people.push(newPerson);

And if you want to access the individual objects/properties,

people[0];
people[0]['name'];

Regarding your subsequent issue, you may want to provide the type definitions for people.

export class AppComponents { 
  // your properties and methods
}

interface Person {
  name: string;
  year: number; 
}

On your main code itself, you can declare the variables with the typings.

const people: Person[] = [{ name: 'robert', year: 1993 }];
const newPerson: Person = { name: 'joe', year: 1992 };
people.push(newPerson);
Sign up to request clarification or add additional context in comments.

7 Comments

I added that and got an error saying Subsequent property declarations must have the same type. Property 'people' must be of type '{name: string; year: number; } [ ] ' , but here has type 'any' . ts (2717)
@RobertoMejia I see.. Hmm, I have updated my code to include the Interface so that we can use it to provide the type definitions for our objects/arrays. Let me know if it solves the issue?
When I placed the interface it gives me an error saying Unexpected token. A constructor, method, accessor or property was expected.
Ok ermm.. Where did you place the interface
I placed it inside the AppComponent class, so inside the class I have, the interface and the last 3 lines of code that you gave me that start with, const people: Person[], const newPerson: Person, and people.push
|
2

Try this

people.push({ name: 'joe', year: 1992 });

6 Comments

I added that and got an error saying Subsequent property declarations must have the same type. Property 'people' must be of type '{name: string; year: number; } [ ] ' , but here has type 'any' . ts (2717)
make sure the person which you are pushing has exact type.
yes just exactly as you have it, I tried it out and it gave me that error
can you share person class code here and what you are pushing that too?
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent { title = 'hi'; people = [{ name: 'robert', year: 1993 }]; people.push({ name: 'joe', year: 1992 });
|

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.