简体   繁体   中英

how i insert a key into a js array to convert to json

I have the following flat array

let data = [
    ["0000001", "SCHOLL", "S", "CEDULA", "NOESTUDIANTE", "GRADO"],
    ["0000002", "MOVIL", "S", "CELULAR", ""],
    ["0000003", "ECHALE", "S", "CONTRATO", "NOCLIENTE", ""],
];

And I have the following function, But this function returns me an array within another array of references, so I would like to be able to assign keys to the values ​​that come flat, I have tried everything, but I don't know very well how to do it, if you could help me please and grateful for your comments

function infoTransform(values) {
    return values.map((value) => {
        let [service, name, ...references] = value;
        service = parseInt([value[0]]);
        references = references.filter((reference) => reference !== "");
        references = references.splice(1);
        let Myjson = JSON.stringify(references);
        return {
            service,
            name,
            Myjson,
        };
    });
}
console.log(infoTransform(data));

I wish it would return something like this to me

let data = [
    {
        id: 1,
        name: "SCHOLL",
        REF1: "CEDULA",
        REF2: "NOESTUDIANTE",
        REF3: "GRADO",
    },
    { id: 2, name: "MOBILE", REF1: "CEDULA" },
    { id: 3, name: "EPALEE", REF1: "NOFOOD", REF2: "FOOBAR" },
];
let data = [
    ["0000001", "SCHOLL", "S", "CEDULA", "NOESTUDIANTE", "GRADO"],
    ["0000002", "MOVIL", "S", "CELULAR", ""],
    ["0000003", "ECHALE", "S", "CONTRATO", "NOCLIENTE", ""],
];

function infoTransform(values) {
    return values.map(([id, name, _, ...refs]) => ({
        id: parseInt(id),
        name,
        ...Object.fromEntries(
            refs.filter(r => r !== "")
                .map((r, i) => [`REF${i + 1}`, r])
        )
    }))
}

console.log(infoTransform(data))

Your function should be something like this:

function infoTransform(values) {
  return values.map((value) => {
    let [service, name, ...references] = value;
    service = parseInt([value[0]]);
    references = references.filter((reference) => reference !== '');
    references = references.splice(1);
    let Myjson = references.reduce((acc, curr, index) => {
      acc[`REF${index + 1}`] = curr;
      return acc;
    }, {});
    return {
      service,
      name,
      ...Myjson,
    };
  });
}

 let data = [ ["0000001", "SCHOLL", "S", "CEDULA", "NOESTUDIANTE", "GRADO"], ["0000002", "MOVIL", "S", "CELULAR", ""], ["0000003", "ECHALE", "S", "CONTRATO", "NOCLIENTE", ""] ]; function infoTransform(values) { return data.map(el => { let id = parseInt(el[0], 10); let obj = {id: id, name: el[1]}; for (let i=3; i<el.length; i++) { if (el[i].length) obj["REF" + (i-2)] = el[i]; }; return obj; }); } console.log(infoTransform(data));

In a parralel time I do that...
.. with Destructuring assignment

 const data = [ [ "0000001", "SCHOLL", "S", "CEDULA", "NOESTUDIANTE", "GRADO"] , [ "0000002", "MOVIL", "S", "CELULAR", ""] , [ "0000003", "ECHALE", "S", "CONTRATO", "NOCLIENTE", ""] ] const infoTransform = arr => arr.map(([id,name,_,...refs])=> ({ id:+id, name, ...refs.filter(x=>!!x) .reduce((a,c,i)=> { a[`REF${i+1}`]=c; return a } ,{})})) console.log( infoTransform(data) )

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