0

I want to get the values of the items then remove the keys not associated to the columns

Below is my code

var columns =['title','code','notes','value','daysCode'];

var items =[{
        title:'Marvel',
        code:'marvel-01',
        notes:'spider-man',
        value:'Value 2',
        daysCode:'Sept 1, 2020',
        daysAge:'Nov 1 , 2019',
        name:'Peter Parker',
        other:'comic'
    },
    {
        title:'DC',
        code:'dc-01',
        notes:'batman',
        value:'Value 1',
        daysCode:'Sept 1, 2020',
        daysAge:'Nov 1 , 2019',
        name:'Bruce Wayne',
        other:'comics'
    },
    {
        title:'Image',
        code:'image-02',
        notes:'spawn',
        value:'Value 3',
        daysCode:'Sept 1, 2020',
        daysAge:'Nov 1 , 2019',
        name:'Albert Simmons',
        other:'comics'
    }
]

Output:

newItems = [{
    title:'Marvel',
    code:'marvel-01',
    notes:'spider-man',
    value:'Value 2',
    daysCode:'Sept 1, 2020'
},
{
   title:'DC',
   code:'dc-01',
   notes:'batman',
   value:'Value 1',
   daysCode:'Sept 1, 2020'
},
{
   title:'Image',
   code:'image-02',
   notes:'spawn',
   value:'Value 3',
   daysCode:'Sept 1, 2020'
}]

How can I do it?

Ivar
  • 4,655
  • 12
  • 45
  • 50
  • Or [ecmascript 6 - Filter object properties by key in ES6 - Stack Overflow](https://stackoverflow.com/questions/38750705/filter-object-properties-by-key-in-es6) – user202729 Feb 16 '21 at 10:11
  • 1
    Your question is a little unclear. You should post the code you have written so far so site users can see what exactly you are trying to do and where it is going wrong. – Hektor Feb 16 '21 at 10:11
  • @user202729 In such cases you mark the question as dupe – Anurag Srivastava Feb 16 '21 at 10:13
  • @AnuragSrivastava I know about that. But since there are more than one I'm just checking which one is better and stuff... (besides I only have a flag) – user202729 Feb 16 '21 at 10:14
  • The easy and short answer would be - const result = items.map(item => { return (({title,code,notes,value,daysCode}) => ({title,code,notes,value,daysCode}))(item) }) console.log("result----",result) – Avinash Rathod Feb 16 '21 at 10:25

1 Answers1

1

Use map along with Object.fromEntries to get your expected output:

var columns =['title','code','notes','value','daysCode'];

var items =[{ title:'Marvel', code:'marvel-01', notes:'spider-man', value:'Value 2', daysCode:'Sept 1, 2020', daysAge:'Nov 1 , 2019', name:'Peter Parker', other:'comic' }, { title:'DC', code:'dc-01', notes:'batman', value:'Value 1', daysCode:'Sept 1, 2020', daysAge:'Nov 1 , 2019', name:'Bruce Wayne', other:'comics' }, { title:'Image', code:'image-02', notes:'spawn', value:'Value 3', daysCode:'Sept 1, 2020', daysAge:'Nov 1 , 2019', name:'Albert Simmons', other:'comics' } ]

var newItems = items.map(o=>Object.fromEntries(columns.map(n=>[n,o[n]])));

// using reduce method:
var newItems1 = items.map(o=>columns.reduce((a,e)=>({...a, [e]:o[e]}),{}));

console.log(newItems);
console.log(newItems1);
gorak
  • 4,839
  • 1
  • 4
  • 16