0

I have a function called syncWithPreviousDay that receives below array of object as a propery.

jsonObj

[
    {
        "Position": "1",
        "TrackName": "Rocket",
        "URL": "https://domain.local/nbs678"
    },
    {
        "Position": "2",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456"
    },
    {
        "Position": "3",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123"
    }
]

Inside this function I need compare the key URL with another array of objects from my database that has the same key pairs and wherever there is a match, move the keys Position and CustomKey to new key PreviousPosition and CustomKey in the data.chart array. If there is no match, create a null value for both keys.

const syncWithPreviousDay = (jsonObj) => {

const data = {
    date: config.dateToday,
    chart: jsonObj
}

dbService.getDate(config.yesterday)
    .then( result => {
        console.log(result.chart)
    })
}

Result from the console.log looks like this:

[
    {
        "Position": "1",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123",
        "CustomKey": "x"
    },
    {
        "Position": "2",
        "TrackName": "Awesome old song",
        "URL": "https://domain.local/123qwe",
        "CustomKey": "y"
    },
    {
        "Position": "3",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456",
        "CustomKey": null
    }
]

So my desired data.chart should somehow look like this:

[
    {
        "Position": "1",
        "TrackName": "Rocket",
        "URL": "https://domain.local/nbs678",
        "PreviousPosition": null,
        "CustomKey": null
    },
    {
        "Position": "2",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456",
        "PreviousPosition": "3",
        "CustomKey": null
    },
    {
        "Position": "3",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123",
        "PreviousPosition": "1",
        "CustomKey": "x"
    }
]

How can this be done?

2 Answers 2

2

You can simply do it by iterating a loop and matching if a particular element exist in database array or not. If values exist then add the required values else add null values.

I have provided the logic to you, you can use it inside the function or any place you need it.

const localArr = [
    {
        "Position": "1",
        "TrackName": "Rocket",
        "URL": "https://domain.local/nbs678"
    },
    {
        "Position": "2",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456"
    },
    {
        "Position": "3",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123"
    }
];

const dbArray = [
    {
        "Position": "1",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123",
        "CustomKey": "x"
    },
    {
        "Position": "2",
        "TrackName": "Awesome old song",
        "URL": "https://domain.local/123qwe",
        "CustomKey": "y"
    },
    {
        "Position": "3",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456",
        "CustomKey": null
    }
];

for (const element of localArr) {
  const index = dbArray.findIndex(item => item.URL === element.URL);
  if (index !== -1) {
    element.PreviousPosition = dbArray[index].Position;
    element.CustomKey = dbArray[index].CustomKey;
  } else {
    element.PreviousPosition = null;
    element.CustomKey = null;
  }
}

console.log(localArr)

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

1 Comment

Thank you very much! I was able to make it work exactly as I wanted it to. All the best
0

Assuming your objects are obj1 and obj2 :

       obj1.map(function (item_1) {
                obj2 = obj2.map(function (item_2) {
                   if(item_1.URL == item_2.URL){
                       item_2['PreviousPosition']=item_1['Position'];
                       item_2['CustomKey']=item_1['CustomKey'];
                   }else{
                       item_2['PreviousPosition']=null;
                       item_2['CustomKey']=null;
                   }
                   return item_2
                });
            });

Comments

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.