Search code examples
javascriptobjectkeyvaluepair

How can I add a key/value pair to a JavaScript object which is in an another object


I'm asking your help for the following code:

function vimeoImport() {

let videosToBeImported = [{
    uri: "/videos/442638455",
    name: "FOMME_1387_VD1",
    modifed_time: "2020-07-29T09:24:48+00:00"
},{
    uri: "/videos/442056086",
    name: "FOMME_1387_VD2",
    modifed_time: "2020-07-29T09:25:27+00:00"
},{
    uri: "/videos/442638455",
    name: "FOMME_2387_VD1",
    modifed_time: "2020-07-29T09:24:48+00:00"
}];

let frtVideoUrlValues = {};

for (var index in videosToBeImported) {
    var videos = videosToBeImported[index];
    let videoName = videos.name;

    let splitName = videoName.split('_');
    let targetedVariationGroup = splitName[0].concat('_', splitName[1]);
    let positionvideo = splitName[2];

    let variationGroupParams = {};
    variationGroupParams[positionvideo] = videos.uri;
    if (targetedVariationGroup in frtVideoUrlValues) {
        frtVideoUrlValues[targetedVariationGroup] += variationGroupParams;
    } else {
        frtVideoUrlValues[targetedVariationGroup] = variationGroupParams;
    }
}

}

I tried to add a key/value pair (the key is a variable) in the targetedVariationGroup object which is in the frtVideoUrlValues object. When I try, I see the new key/value pair in the targetedVariationGroup but the merge is not functional and the 2 objects are not accessible:

enter image description here

And I try to obtain an object like this:

enter image description here


Solution

  • As, you didn't provide any output format, I am guessing the output should look like as follows(If this is not what you've wanted, pls provide proper output format):

    {
      FOMME_1387: [
        { VD1: '/videos/442638455' },
        { VD2: '/videos/442056086' }
      ],
      FOMME_2387: [
        { VD1: '/videos/442638455' }
      ]
    }
    

    Now, to achieve this you should write code as follow:

    function vimeoImport() {
        let videosToBeImported = [
            {
                uri: "/videos/442638455",
                name: "FOMME_1387_VD1",
                modifed_time: "2020-07-29T09:24:48+00:00"
            },
            {
                uri: "/videos/442056086",
                name: "FOMME_1387_VD2",
                modifed_time: "2020-07-29T09:25:27+00:00"
            },
            {
                uri: "/videos/442638455",
                name: "FOMME_2387_VD1",
                modifed_time: "2020-07-29T09:24:48+00:00"
            }
        ];
    
    
        let frtVideoUrlValues = {};
    
        for (var index in videosToBeImported) {
            var videos = videosToBeImported[index];
            let videoName = videos.name;
    
            let splitName = videoName.split('_');
            let targetedVariationGroup = splitName[0].concat('_', splitName[1]);
            let positionvideo = splitName[2];
    
            let variationGroupParams = {};
            variationGroupParams[positionvideo] = videos.uri;
            
            // here are the changes I've made
            if(frtVideoUrlValues[targetedVariationGroup] === undefined) {
                frtVideoUrlValues[targetedVariationGroup] = [];
            }
    
            frtVideoUrlValues[targetedVariationGroup].push(variationGroupParams);
        }
    
        console.log(frtVideoUrlValues);
    }
    
    vimeoImport();
    

    The problem with your code is, you're using + to add object with another object, but + is only used to concat string in javascript. Instead of what you're doing, you should push objects into array. To add new elements in an array, you've to use push() method.

    Also, notice, if key targetedVariationGroup in frtVideoUrlValues is undefined, I've assigned an empty array to targetedVariationGroup as follows:

    frtVideoUrlValues[targetedVariationGroup] = [];
    

    and then, pushed variationGroupParams object in the array as follows:

    frtVideoUrlValues[targetedVariationGroup].push(variationGroupParams);