简体   繁体   中英

How to get matched name from array key name in javascript

Tried to get matched name from the array key name but not working. I have two array-like columns and item. I do not know how to match the array key name to column name words and assign it to the field value. If anyone knows please help to find the solutions.

var column = ['File Name', 'Sur Name', 'Last Name', 'Place Of Work', 'Work Id']; 
var newColumn = []; 
var item = [
    {
        fileName: 'test1',
        surName: 'test1',
        lastName: 'test1',
        placeOfWork: 'test1',
        workId: 'test1'
    }
];

column.forEach(e => {
    newColumn.push({
        label: e,
        field: function(){
            if(item.keys( e.toLowerCase() == item.keys().toLowerCase())) return item.keys(); 
        }
    });
});

console.log(newColumn);

Output Should be:

newSolumn= [
    {label:'First Name',field:'fileName'},
    {label:'Sur Name',field:'surName'}, 
    {label:'Last Name',field:'lastName'},
    {label:'Place Of Work',field:'placeOfWork'},
    {label:'Work Id',field:'workId'}
];

Demo: https://stackblitz.com/edit/js-gatg4v

You don't need a function as a value in your created object, you need to call it instead of assigning it as a value, and you need also to remove spaces in your strings in order to compare them, I use the function replace to remove useless spaces.

var column = ['File Name', 'Sur Name', 'Last Name', 'Place Of Work', 'Work Id'];

var newColumn = [];

var item = [
  {
    fileName: 'test1',
    surName: 'test1',
    lastName: 'test1',
    placeOfWork: 'test1',
    workId: 'test1'
  }
];

column.forEach(e => {
  newColumn.push({
    label: e,
    field: getField(e)
  });
});

function getField(columnName) {
  for (key in item[0]) {
    if (
      columnName.replace(/\s/g, '').toLowerCase() ===
      key.replace(/\s/g, '').toLowerCase()
    ) {
      return key;
    }
  }
}

console.log(newColumn);

This part is wrong:

field: function(){
  if(item.keys( e.toLowerCase() == item.keys().toLowerCase())) return item.keys(); 
}

Some issues with it:

  • you don't want field to be a function. It should be a string value. So if there is a condition to evaluate, you should evaluate it before creating the target object, or else filter the result to remove those where the field could not be matched.

  • item.keys() takes no argument, so the expression you have there will not serve any purpose. To match a property in item , you would better have those lower cased from the start, so you can take advantage of direct lookup. So item could have key/value pairs like:

     placeofwork: "placeOfWork"

    Now you can just do item[e.toLowerCase()]

  • The spaces should be removed from e before you try to match.

Here is a correction:

 let column = ['File Name', 'Sur Name', 'Last Name', 'Place Of Work', 'Work Id']; let item = Object.fromEntries( ["fileName", "surName", "lastName", "placeOfWork", "workId"].map(field => [field.toLowerCase(), field]) ); let newColumn = column.map(label => ({ label, field: item[label.toLowerCase().replace(/\s/g, "")] })).filter(o => o.field); console.log(newColumn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM