J'ai l'objet de valeurs suivant:

const data = {
    "generalInfo": [{
            "title": "title1",
            "permalink": "www.link.com",
            "manufacturer": "manufacturer1",
            "category": [{
                "term_id": 35,
                "name": "Motherboard",
                "slug": "motherboard"
            }],
            "img": "https://images-na.ssl-images-test.com/images/asdfIdR/5adf1vELadfZeiMML.jpg",
            "curreny": "$",
            "price": "64.00",
            "availability": "Usually ships in 24 hours",
        },
        {
            "title": "title2",
            "permalink": "www.link.com",
            "manufacturer": "manufacturer2",
            "category": [{
                "term_id": 35,
                "name": "Motherboard",
                "slug": "motherboard"
            }],
            "img": "https://images-na.ssl-images-test.com/images/I/51adfkLhadsfgACH0L.jpg",
            "curreny": "$",
            "price": "59.99",
            "availability": "Usually ships in 24 hours",
        }
    ]
}

// console.log(typeof(data))

var vals = Object.keys(data).map(function(key) {
    return data[key]
})

console.log(vals)

// expected output
// [ "1", "title1", "manufacturer1", "64.00", "Usually ships in 24 hours", "", "" ],
// [ "2", "title2", "manufacturer2", "59.99", "Usually ships in 24 hours", "", "" ],

J'essaie d'utiliser Object.keys(data).map pour créer une structure de tableau à partir de mon objet. Cependant, je reçois un tableau dans un tableau avec 2 objets. Cependant, je recherche la sortie suivante:

// expected output
// [ "1", "title1", "manufacturer1", "64.00", "Usually ships in 24 hours", "", "" ],
// [ "2", "title2", "manufacturer2", "59.99", "Usually ships in 24 hours", "", "" ],

Des suggestions sur la façon de transformer le tableau pour obtenir la sortie ci-dessus?

J'apprécie vos réponses!

-1
Anna.Klee 15 avril 2018 à 12:03

3 réponses

Meilleure réponse

Ce que vous voulez n'a rien à voir avec Object.keys. Vous pouvez utiliser map directement sur la touche generalInfo:

const data = {"generalInfo":[{"title":"title1","permalink":"www.link.com","manufacturer":"manufacturer1","category":[{"term_id":35,"name":"Motherboard","slug":"motherboard"}],"img":"https://images-na.ssl-images-test.com/images/asdfIdR/5adf1vELadfZeiMML.jpg","curreny":"$","price":"64.00","availability":"Usually ships in 24 hours"},{"title":"title2","permalink":"www.link.com","manufacturer":"manufacturer2","category":[{"term_id":35,"name":"Motherboard","slug":"motherboard"}],"img":"https://images-na.ssl-images-test.com/images/I/51adfkLhadsfgACH0L.jpg","curreny":"$","price":"59.99","availability":"Usually ships in 24 hours"}]};
let vals = data.generalInfo.map((item, i) => [i + 1, item.title, item.manufacturer, item.price, item.availability])
console.log(vals);

Je ne sais pas ce que signifient les deux dernières valeurs, alors omettez-les. Vous pouvez inclure ceux similaires à ce que j'ai fait.

1
31piy 15 avril 2018 à 09:07

Vous devez utiliser Object.values() au lieu de Object.keys() pour obtenir la valeur de chaque objet dans le data.generalInfo dans une nouvelle structure de tableau:

const data = {
    "generalInfo": [{
            "title": "title1",
            "permalink": "www.link.com",
            "manufacturer": "manufacturer1",
            "category": [{
                "term_id": 35,
                "name": "Motherboard",
                "slug": "motherboard"
            }],
            "img": "https://images-na.ssl-images-test.com/images/asdfIdR/5adf1vELadfZeiMML.jpg",
            "curreny": "$",
            "price": "64.00",
            "availability": "Usually ships in 24 hours",
        },
        {
            "title": "title2",
            "permalink": "www.link.com",
            "manufacturer": "manufacturer2",
            "category": [{
                "term_id": 35,
                "name": "Motherboard",
                "slug": "motherboard"
            }],
            "img": "https://images-na.ssl-images-test.com/images/I/51adfkLhadsfgACH0L.jpg",
            "curreny": "$",
            "price": "59.99",
            "availability": "Usually ships in 24 hours",
        }
    ]
}

// console.log(typeof(data))

var vals = data.generalInfo.map(function(obj) {
    return Object.values(obj);
})

console.log(vals)

// expected output
// [ "1", "title1", "manufacturer1", "64.00", "Usually ships in 24 hours", "", "" ],
// [ "2", "title2", "manufacturer2", "59.99", "Usually ships in 24 hours", "", "" ],
1
Ankit Agarwal 15 avril 2018 à 09:08

Vous pouvez prendre un tableau de clés pour l'ordre souhaité des articles.

var data = { generalInfo: [{ title: "title1", permalink: "www.link.com", manufacturer: "manufacturer1", category: [{ term_id: 35, name: "Motherboard", slug: "motherboard" }], img: "https://images-na.ssl-images-test.com/images/asdfIdR/5adf1vELadfZeiMML.jpg", curreny: "$", price: "64.00", availability: "Usually ships in 24 hours" }, { title: "title2", permalink: "www.link.com", manufacturer: "manufacturer2", category: [{ term_id: 35, name: "Motherboard", slug: "motherboard" }], img: "https://images-na.ssl-images-test.com/images/I/51adfkLhadsfgACH0L.jpg", curreny: "$", price: "59.99", availability: "Usually ships in 24 hours" }] },
    keys = ['title', 'manufacturer', 'price', 'availability'],
    result = data.generalInfo.map((o, i) => [(i + 1).toString(), ...keys.map(k => o[k]), '', '']);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
Nina Scholz 15 avril 2018 à 09:16