Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Concatenate each object property values of javascript array , properties are list of strings in javascript

i have array of objects need to convert to new array with new property using another list of strings to read the property in javascript.

var output =

[
 {Id: '000000CayeAAC', Name: 'KCP13', FTA: 'LOS', FTN: 'M'}
 {Id: '000000CayLAAS', Name: 'KCN15', FTA: 'DC', FTN: 'M'}
 {Id: '000000CaxXAAS', Name: 'KCA21', FTA: 'AUS', FTN: 'M'}
 {Id: '000000CaxCAAS', Name: 'KCZ43', FTA: 'CA', FTN: 'M'}
]

var mypropertylist = ['Name','FTA','FTN'];

i want to new array is like below

var newArray =

 [ 
  {value: '000000CayeAAC', concatName: 'KCP13 - LOS - M'}
  {value: '000000CayLAAS', concatName: 'KCN15 - DC - M'}
  {value: '000000CaxXAAS', concatName: 'KCA21 - AUS - M'}
 ]

i tried this way

output.map(item=>{
                    
                    this.newArray  = [...this.newArray,
                                            {value:item.Id, 
                                             concatName:item.Name + ' - '+item.FTA+ ' - '+item.FTN}];

i got desired output but i want to get dynamically by using mypropertylist

Thanks in Advance!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If I understand it, the property list is a list of keys whose values should be concatenated to form concatName. Let's do that just that property manipulation first...

// return a string containing the values of props in object, concatenated 
function concatValues(object, props) {
  const values = props.map(prop => object[prop]);
  return values.join(' - ');
}

Next we need a thing that transforms the object, changing the Id prop and using the concatValues...

// return an object with it's Id renamed and it's props concatenated
function transform(object, props) {
  return { value: object.Id, concatName: concatValues(object, props) }
}

Then, just map that over the input. Demo...

// return a string containing the values of props in object, concatenated 
function concatValues(object, props) {
  const values = props.map(prop => object[prop]);
  return values.join(' - ');
}

// return an object with it's Id renamed and it's props concatenated
function transform(object, props) {
  return { value: object.Id, concatName: concatValues(object, props) }
}

const arrayA = [
 {Id: '000000CayeAAC', Name: 'KCP13', FTA: 'LOS', FTN: 'M'},
 {Id: '000000CayLAAS', Name: 'KCN15', FTA: 'DC', FTN: 'M'},
 {Id: '000000CaxXAAS', Name: 'KCA21', FTA: 'AUS', FTN: 'M'},
 {Id: '000000CaxCAAS', Name: 'KCZ43', FTA: 'CA', FTN: 'M'}
];

const mypropertylist = ['Name','FTA','FTN'];
const arrayB = arrayA.map(el => transform(el, mypropertylist))
console.log(arrayB);

about 4 years ago · Juan Pablo Isaza Report

0

that ?

const output = 
  [ { Id: '000000CayeAAC', Name: 'KCP13', FTA: 'LOS', FTN: 'M' } 
  , { Id: '000000CayLAAS', Name: 'KCN15', FTA: 'DC',  FTN: 'M' } 
  , { Id: '000000CaxXAAS', Name: 'KCA21', FTA: 'AUS', FTN: 'M' } 
  , { Id: '000000CaxCAAS', Name: 'KCZ43', FTA: 'CA',  FTN: 'M' } 
  ] 

const concatFunction = (arr, ...items) =>
  arr.map(({Id,...elms}) =>
    ({ value: Id
     , concatName: items.map( item => elms[item] ).join(' - ') 
    })
  )

console.log( concatFunction( output, 'Name', 'FTA', 'FTN' ))
.as-console-wrapper { max-height: 100% !important; top: 0; }
.as-console-row::after { display: none !important; }

about 4 years ago · Juan Pablo Isaza Report

0

var output = [
 {Id: '000000CayeAAC', Name: 'KCP13', FTA: 'LOS', FTN: 'M'},
 {Id: '000000CayLAAS', Name: 'KCN15', FTA: 'DC', FTN: 'M'},
 {Id: '000000CaxXAAS', Name: 'KCA21', FTA: 'AUS', FTN: 'M'},
 {Id: '000000CaxCAAS', Name: 'KCZ43', FTA: 'CA', FTN: 'M'}
];

var newArray = output.map(item => {
    return {Id: item.Id, concatName: [item.Name, item.FTA, item.FTN].join(" - ")};
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!