0

I have a function that receives data from a previous JSON response, calls another web service, and adds the results from the second call to the end of the original JSON response. However, the items in my original array get replaced by items in the new array when I used extend. I understand that using $.extend will override my items with identical keys, but I cannot find another option as I am not trying to $.merge my items as I need to concatenate them. Is there any other viable option? Thanks!

//data is the object to which I am trying to add data
function verificaFileServer(data, subprograma, etapa, destino, lista, listaFileServer){

    //checking whether some variable has been passed into the function
    if(listaFileServer !== undefined){

        //SharePoint REST Endpoint
        var urlREST = "/_api/lists/getbytitle('File Server')/items?" + 
                      "$select=Title,Link,Modified&$orderby=Modified desc&$filter=IDdoEvento eq " + subprograma + " and EtapaPAS eq '" + etapa + "' and TipodeArquivo eq '" + listaFileServer + "'";

        $.ajax({
            url: urlREST,
            headers: { "Accept": "application/json; odata=verbose"},
            success:function(dadosFileServer) {

                //trying to add to the end of my original JSON object
                $.extend(data.d.results,dadosFileServer.d.results);

                //creating a new array containing the original and the new data
                dadosConcatenados = data.d.results.sort(function(a, b) {
                    return (b["Modified"] > a["Modified"]) ? 1 : ((b["Modified"] < a["Modified"]) ? -1 : 0);
                });

                //calling a function that will build the output based on the concatenated array
                montaSaida(dadosConcatenados, subprograma, etapa, lista, destino);

            }
        });

    } else {
        //calling a function that will build the output based on the original data
        montaSaida(data, subprograma, etapa, lista, destino);
    }

}
3
  • "am not trying to $.merge my items as I need to concatenate them" Can include examples of original json data , expected final json data ? Commented Dec 2, 2015 at 0:12
  • 1
    SharePoint adds a lot of trash to my REST response. I'll try to get the important data. Commented Dec 2, 2015 at 0:13
  • Have you seen this post: stackoverflow.com/questions/433627/concat-json-objects ? Commented Dec 2, 2015 at 0:16

1 Answer 1

2

However, the items on the original array get replaced by items in the new array.

Try using Array.prototype.concat()

var data = [1, 2, 3];

data = data.concat(4, 5, 6, 7, {"abc": [8, 9]});

console.log(data)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that did the trick! According to David Tansey's comment, my question is a possible duplicate. Should I accept the answer or delete my question?
@LucasRodrigues "Thanks, that did the trick! According to David Tansey's comment, my question is a possible duplicate. Should I accept the answer or delete my question?" Not certain ? Use own discretion , judgment ? Was not aware of previous Question , here.
@LucasRodrigues Note , could also use $.merge() if pass array as last parameter ; e.g., var data = [1, 2, 3]; data = $.merge(data, [4, 5, 6, 7, {"abc": [8, 9]}], []); console.log(data) . Though $.merge() only concatenates first two parameters passed , ignores more than two values to concatenate ; for workaround could add all values to be concatenated to single array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.