0

I am retrieving list item values in sharepoint using REST API.

Now I want to store these values in JSON so I can just call the JSON and retrieve the values in another function

$.ajax({
    url: lib,
    method: 'GET',
    async: false,
    headers: {"accept":"application/json;odata=verbose"},
    success: function(data) {
        /* console.log(data); */
        if(data.d != undefined) {
            var temp_prod= [];
            $.each(data.d.results, function (i,item) {

            }//each
        } });

I want to construct the JSON to be like this

var products = [{ Dell Laptop: { price: 1000.00, color: black },
                  HP Laptop: { price: 900.00, color: grey }]

and can I access these using

var dellLaptopPrice = products["Dell Laptop"].price;
var dellLaptopColor = products["Dell Laptop"].color;
0

got it using this code

var products = {}

$.each(data.d.results, function (i,item) {
      products[item.Name] = {name: item.Name, price: item.Price, color: item.Color }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.