Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
find key and replace value with it in javascript / node js

let data = [{
    system: {
      id: "4gSSbjCFEorYXqrgDIP2FA",
      type: "Entry",
      name: "User"
    },
    DataDetails: {
      shortOption: {
        "en-us": "some value"
      },
      mediaFile: [{
          sys: {
            type: "Link",
            link: "Entry",
            id: "7kRzyt4PFo",
          },
        },
        {
          sys: {
            type: "Link",
            link: "Entry",
            id: "2OspeCtNK0s",
          },
        },
      ],
      mediaGalary: [{
          sys: {
            type: "Link",
            link: "Asset",
            id: "gHcw3Z1Ko",
          },
        },
        {
          sys: {
            type: "Link",
            linkType: "Asset",
            id: "h2cPiuU9jIz",
          },
        },
      ],
        mediaExplore: [{
          sys: {
            type: "Link",
            link: "Asset",
            id: "euwcbds3282",
          },
        },
        {
          sys: {
            type: "Link",
            linkType: "Asset",
            id: "jndsiw23912",
          },
        },
      ],
      singleMediaImage: {
        sys: {
          type: "Link",
          linkType: "Asset",
          id: "7kRzyt4PFo",
        },
      },
    },
  },
  {
    system: {
      id: "1aBOO8tu3lUsjtICuIbUM5",
      type: "Entry",
      name: "User"
    },
    DataDetails: {
      short: {
        "en-us": "details of shorts"
      },
      shortSlugOption: {
        "hi-In": "options"
      },
      booleanField: {
        kl: "true"
      },
    },
  },
  {
    system: {
      id: "2pOUGnI1oRD7nsrYs600HA",
      type: "Entry",
      name: "Dummy"
    },
    DataDetails: {
      testingNewValue: [{
          sys: {
            type: "Link",
            link: "Entry",
            id: "66rzYr2BpWL",
          },
        },
        {
          sys: {
            type: "Link",
            link: "Entry",
            id: "1VTBHdLTdSW",
          },
        },
      ],
    },
  },
  {
    system: {
      id: "66rzYr2BpWL1VTBHdLTdSW",
      type: "Entry",
      name: "new"
    },
    DataDetails: {
      oneReference: {
        sys: {
          type: "Link",
          linkType: "Asset",
          id: "h2cPiuU9jIz",
        },
      },
      multiReference: [{
          sys: {
            type: "Link",
            link: "Asset",
            id: "gHcw3Z1Ko",
          },
        },
        {
          sys: {
            type: "Link",
            link: "Asset",
            id: "h2cPiuU9jIz",
          },
        },
      ],
    },
  },
  {
    system: {
      id: "cIb5mqEBRWDD6hrNmFmFE",
      type: "Entry",
      name: "new"
    },
    DataDetails: {
      testingNewValue: {
        "hi-IN": "jksdsdo"
      }
    },
  },
  {
    system: {
      id: "7kRzyt4PFrX13gHcw3Z1Ko",
      type: "Entry",
      name: "Dummy"
    },
    DataDetails: {
      testingNewValue: {
        "en-us": "kknksdo"
      }
    },
  },
];

let anotherObj = {
  "gHcw3Z1Ko": {
    status: true,
    tag: [],
    filename: "exute-image.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "h2cPiuU9jIz": {
    status: true,
    tag: [],
    filename: "wallpapers-6.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "7kRzyt4PFo": {
    status: true,
    tag: [],
    filename: "in-space-rk.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "euwcbds3282": {
    status: true,
    tag: [],
    filename: "justice-league.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "jndsiw23912": {
    status: true,
    tag: [],
    filename: "batman.jpg",
    is_dir: false,
    parent_uid: null,
  },
};

let res = data.reduce((acc, curr) => {
  if (!acc[curr.system.name]) {
    acc[curr.system.name] = {};
  }

  let detailsObj = {};
  let assetArray=[];
  for (let key of Object.keys(curr.DataDetails)) {
    detailsObj[key] = Object.values(curr.DataDetails[key])[0];
    if (typeof Object.values(curr.DataDetails[key])[0] === "object") {
      Object.values(curr.DataDetails[key]).map((type) => {
        if (Array.isArray(type)) {
          type.map((link) => {
            if (link.sys.link === "Asset") {
              for (let id in anotherObj) {
                if (link.sys.id === id) {
                assetArray.push(anotherObj[id]);
                  detailsObj[key] = assetArray;
                }
              }
            }                
          });
        } else {
          if (type.sys !== undefined) {         
            // for single Asset
            if (type.sys.link === "Asset") {            
              for (let id in anotherObj) {
                if (type.sys.id === id) {  
                 detailsObj[key] = [anotherObj[id]];
                }
              }
            }
          }
        }
      });
    }
  }

  acc[curr.system.name][curr.system.id] = {
    title: curr.system.id,
    uid: curr.system.id,
    url: `/${curr.system.name}/${curr.system.id}`,
    ...detailsObj,
  };
  return acc;
}, {});

console.log(res);

I am trying to put two object data in one output but by reading the link which is Asset

but in the multiple Array list the output is displaying as single Asset output as mediaFile contain two entries but I don't know where I am doing wrong and it's displaying me a single value

as my main purpose is to replace the sys:{id:erfrdvsdf} by the anotherObj key data

so it should look like this for example my output is like this

"User": {
    "4gSSbjCFEorYXqrgDIP2FA": {
      "title": "4gSSbjCFEorYXqrgDIP2FA",
      "uid": "4gSSbjCFEorYXqrgDIP2FA",
      "url": "/User/4gSSbjCFEorYXqrgDIP2FA",
      "shortOption": "some value",
      "mediaFile": {
        "sys": {
          "type": "Link",
          "link": "Entry",
          "id": "7kRzyt4PFo"
        }
      },
      "mediaGalary": [
        {
          /**id:7**/
          "status": true,
          "tag": [],
          "filename": "in-space-rk.jpg",
          "is_dir": false,
          "parent_uid": null
        }
      ],
      "singleMediaImage": {
        "type": "Link",
        "linkType": "Asset",
        "id": "h2cPiuU9jIz"
      }
    },
  },

but it should like this

"User": {
    "4gSSbjCFEorYXqrgDIP2FA": {
      "title": "4gSSbjCFEorYXqrgDIP2FA",
      "uid": "4gSSbjCFEorYXqrgDIP2FA",
      "url": "/User/4gSSbjCFEorYXqrgDIP2FA",
      "shortOption": "some value",
      "mediaFile": {
        "sys": {
          "type": "Link",
          "link": "Entry",
          "id": "7kRzyt4PFo"
        }
      },
      "mediaGalary": [
        {
          "status": true,
          "tag": [],
          "filename": "in-space-rk.jpg",
          "is_dir": false,
          "parent_uid": null
        },
        {
          "status": true,
          "tag": [],
          "filename": "wallpapers-6.jpg",
          "is_dir": false,
          "parent_uid": null
        },
      ],
      "mediaExplore": [
        {
          "status": true,
          "tag": [],
          "filename": "justice-league",
          "is_dir": false,
          "parent_uid": null
        },
        {
          "status": true,
          "tag": [],
          "filename": "batman.jpg",
          "is_dir": false,
          "parent_uid": null
        },
      ],
      "singleMediaImage": [
        {
          "status": true,
          "tag": [],
          "filename": "wallpapers-6.jpg",
          "is_dir": false,
          "parent_uid": null
        },
      ]
    },
  },

but my expected output is what I am looking to it

Due to this I am stuck with the entry field as the same problem I am going through with entry field as I wrote same code to fetch the entry fields which are in Array and for the single entry field

where my code break I don't know

I know this is long code but help me out to solve this error as due to this I am not moving ahead

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are missing some values because you don't check for linkType. Your loops are checking for sys.link but aren't checking for sys.linkType.

Use:

link.sys.link === 'Asset' || link.sys.linkType === 'Asset'

and

type.sys.link === 'Asset' || type.sys.linkType === 'Asset'

Also this line will override your array: detailsObj[key] = [anotherObj[id]];

Replace it with:

assetArray.push(anotherObj[id]);
detailsObj[key] = assetArray;

To be clear this code isn't good. A few suggestions:

  • Split your code in smaller logic units. Don't nest 5 levels of loops. Make functions that do small operations and name them accordingly (e.g. instead of doing for (let id in anotherObj) { ... }, put the logic inside a function named findAssetData)
  • Understand the difference between Array.prototype.forEach and Array.prototype.map (map usage in this code is wrong)
  • Use code quality tools like eslint, it will help if you are a beginner
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda