Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Angular2,Typescript:How to push array of objects into another array of objects with only a few fields of the object

I know how to operate on array of objects but never had the necessity to push one array data into another. I need to push an array of objects into another array with only 2 fields of the object. Right now my object format is somewhat like this

data: [{
        "name": "Btech",
        "courseid": "1",
        "courserating": 5,
        "points": "100",
        "type": "computers"
    },
   {
        "name": "BCom",
        "courseid": "2",
        "courserating": 5,
        "points": "100",
        "type": "computers"
    }];

I want to push this into another array but I want only courseid and name in the object. I've read that we need to initialise the object in the constructor, use slice() and a few other functions and then push but I don't know how can I do it for mine since I need to push one array data into another.Kindly someone help me in this regard.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You're looking for the array map() method:

const newArray = array.map(o => {
  return { name: o.name, courseid: o.courseid };
});
over 4 years ago · Santiago Trujillo Relatório

0

Try this:

let data = [{
    "name": "Btech",
    "courseid": "1",
    "courserating": 5,
    "points": "100",
    "type": "computers"
},
{
    "name": "BCom",
    "courseid": "2",
    "courserating": 5,
    "points": "100",
    "type": "computers"
}];

let other = []; // your other array...

data.map(item => {
    return {
        courseid: item.courseid,
        name: item.name
    }
}).forEach(item => other.push(item));

console.log(JSON.stringify(other))
// => [{"courseid":"1","name":"Btech"},{"courseid":"2","name":"BCom"}]
over 4 years ago · Santiago Trujillo Relatório

0

You can simply do it like this.

//assign your array of object to variable

var youArray:Array<any>= [{
    "name": "Btech",
    "courseid": "1",
    "courserating": 5,
    "points": "100",
    "type": "computers"
},
{
    "name": "BCom",
    "courseid": "2",
    "courserating": 5,
    "points": "100",
    "type": "computers"
}];

var resultArray:Array<any>=[] //empty array which we are going to push our selected items, always define types 

youArray.forEach(i=>{ 
   resultArray.push(
   {
    "name":i.name,
    "courseid":i.courseid
   });
});

console.log(resultArray)

if you still have doubts about this.please follow this url

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda