2021年5月5日星期三

Compare and replace value in array of object with array of object

Suppose I have an array of object which has some false data:

let bookInfo = [      {          "author_details": { "author": "RR Tolkiens", "authoAddress": "South A erica", "age":46 }      },      {          "book_details": [              { "AMOUNT": "298.19 ", "BOOK DESCRIPTION": "STURDY," },              { "AMOUNT": "386.28",  "DESCRIPTION": "HARDCOVER" }          ],          "header_details": [              {"name":"library","title":"Library"},              {"name":"author","title":"Author"}                ]      }  ]  

P.S. 1: Order of bookInfo is always fixed i.e. author_details, book_details, header_details always remain at same index

P.S. 2: book_details can have multiple keys, BOOK DESCRIPTION, DESCRIPTION, anything that starts with desc, BOOK DESC

P.S. 3: For author_detail , I only want to replace value for author and authoAddress not for age, if oldvalue matches with current value of author_details' author and authoAddress

And another array of object as from which we have to replace value:

let replacementData = [ { 'title': 'bookDescription','oldData': 'STURDY', 'newData': 'BISMONTH'},                          { 'title': 'author','oldData': 'RR Tolkiens','newData': 'JRR Tolkiens'},                           {'title': 'authoAddress','oldData': 'South A erica', 'newData': 'South America'},                          {'title': 'age','oldData': '46', 'newData': '53'},                           { 'title': 'author','oldData': 'APPLE BEE','newData': 'HYUMA'},                          {'title': 'authoAddress','oldData': 'SRI LANKA', 'newData': 'ALBENIA'} ]  

I want to loop through bookInfo and check for values in replacementData, if old value of replacement data matches with current value of author_details' author and authoAddress, then we replace author_details' value with new value. And same we look for book_details' description and if they match with old value of replacementData replace that book_details' description with new value.

For this I tried:

for(let i = 0;i<bookInfo.length;i++){      for(let j = 0; j< replacementData.length;j++) {                    if( bookInfo[0].author_details.author === replacementData[j].oldData) {                 bookInfo[0]['author_details']['author'] = replacementData[j].newData          } else if( bookInfo[0].author_details.authoAddress === replacementData[j].oldData) {                bookInfo[0]['author_details']['authoAddress'] = replacementData[j].newData          } else if ( bookInfo[1].book_details.length > 0 ) {                        for(let k = 0; k<bookInfo[1].book_details.length; k++){                                Object.keys(bookInfo[1].book_details[k]).forEach(e => {                         if (e.startsWith('Description') || e.startsWith('desc') || e.startsWith('ITEM DE') || e.startsWith('DESCRIPTION')) {                                                  }                  })              }          }      }  }  

But unable to move from here, could someone suggest me how to move forward from here.

My expected O/P : console.log(bookInfo) =>

[      {          "author_details": { "author": "JRR Tolkiens", "authoAddress": "SouthmAerica", "age":46 }      },      {          "book_details": [              { "AMOUNT": "298.19 ", "BOOK DESCRIPTION": "BISMONTH," },              { "AMOUNT": "386.28",  "DESCRIPTION": "HARDCOVER" }          ],          "header_details": [              {"name":"library","title":"Library"},              {"name":"author","title":"Author"}                ]      }  ]  

P.S. 4: No change or replace needed for header_details

I know this is a bit long and I might not be clear, if anyone needs any more details please let me know.

https://stackoverflow.com/questions/67411146/compare-and-replace-value-in-array-of-object-with-array-of-object May 06, 2021 at 10:42AM

没有评论:

发表评论