0

I have an array of objects in TypeScript(Angular), and I want only names from this array if details array is not empty

 [
  {name: "Joe", detail: []},
  {name: "Kevin", detail: [{salary: 2400}] },
  {name: "Peter", detail: [{salary: 2100}]}
]

I want this result Kevin,Peter in one variable

Currently I have done this

 [
  {name: "Joe", detail: []},
  {name: "Kevin", detail: [{salary: 2400}] },
  {name: "Peter", detail: [{salary: 2100}]}
].map(function(elem){
return elem.name;}).join(",")

Can anyone help me to do this in Typescript or JavaScript?

1
  • 2
    check out Array#filter() Commented Jan 21, 2021 at 9:20

2 Answers 2

1

const array = [
  { name: "Joe", detail: [] },
  { name: "Kevin", detail: [{ salary: 2400 }] },
  { name: "Peter", detail: [{ salary: 2100 }] },
];

const filteredArray = array.filter((obj) => {
  return obj.detail.length > 0;
});

const result = filteredArray.map((obj) => obj.name).join(", ");
console.log(result);

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

5 Comments

Don't need to loop 2 times like this, bro.
@Phong I've read your answer and it's perfect. Imran is a new contributor so I thought we should help him to give him as many options as we can.
@Phong That's very much to preference, and a tradeoff between performance and readability/maintainability. I find array.filter(obj => obj.details.length > 0).map(obj => obj.name) more clear than your approach with reduce (and I've written code like that myself in the past) or if you don't want to loop twice over the array, I'd still go with a loop const result = []; for(const obj of array) if(obj.details.length > 0) result.push(obj.name); (properly formatted) over of the reduce. But as I said, it's personal preference.
@Thomas very true but compatibility is a problem with for...of loop. As it is not supported by IE and every Array methods are compatible with all browser.
@DeC This issue died a few years ago with the spread of build-pipelines and transpilers.
1

I do not want Joe in the result because Joe's detail is empty. I only want kevin,Peter

You can use .reduce combines with destructure object like below to resolve it.

var data =  [
  {name: "Joe", detail: []},
  {name: "Kevin", detail: [{salary: 2400}] },
  {name: "Peter", detail: [{salary: 2100}]}
];

var result = data.reduce((acc, {name, detail}) => {
  if(detail.length > 0) acc.push(name) // condition to add name into result   
  return acc;
}, []);
console.log(result.join(","));

5 Comments

I do not want Joe in result because Joe details is empty. I only want kevin,Peter
Updated my answer, pls take a look at ^^!
Is there any better approach we can do in Angular
Oops. It's the same. The original is Javascript code instead of the framework or library, bro :)
concise and efficient answer.

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.