-3

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!

3

3 Answers 3

1

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);

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

Comments

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; }

1 Comment

Your answer would benefit from an explanation of what your code is doing.
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(" - ")};
});

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.