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!
CodePudding user response:
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);
CodePudding user response:
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; }
CodePudding user response:
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(" - ")};
});