0

How to add an array dynamically as in the code below. I have created a multi-dimensional array in JavaScript but how do I push src that is "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg" and title that is "MyTitle". I have multiple values and I am iterating through them?

carousel_images_info = [
    { src: "/_layouts/15/My.Portal/Images/my1.jpg", title: "ssnit01" },
    { src: "/_layouts/15/my3.Portal/Images/my2.jpg", title: "ssnit02" },
    { src: "/_layouts/15/my4.Portal/Images/my3.jpg", title: "ssnit03" },
    { src: "/_layouts/15/my5.Portal/Images/my4.jpg", title: "ssnit04" },
    { src: "/_layouts/15/my5.Portal/Images/ssnit05.jpg", title: "ssnit05" }
];
5
  • Please note that the problem has nothing to do with JSON at all. You are confusing JavaScript object literals (a construct of the JavaScript language syntax) with JSON (a language-independent data-exchange format, like XML or CSV). I will edit your question accordingly. Commented Sep 5, 2013 at 8:54
  • Array.prototype.concat create one array from two simply adding each entry from right to left. Commented Sep 5, 2013 at 8:54
  • @Eraden can you show me an example? supposing I have these values above? Commented Sep 5, 2013 at 8:57
  • I recommend to read these resources to learn the basics about arrays and objects. Commented Sep 5, 2013 at 8:57
  • var a = [1,2,3,4]; var b = [1,2,3]; var c = a.concat(b); c is now [1,2,3,4,1,2,3]; Of course members of array may be everything. But if you want add only one member use push like bellow. Commented Sep 5, 2013 at 9:11

4 Answers 4

1

That is array containing objects, you need to push object to it, like:

var new_obj = {
    src: "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg",
    title: "My Title"
};
carousel_images_info.push(new_obj);
Sign up to request clarification or add additional context in comments.

Comments

1

Just push :)

carousel_images_info.push({ 
    title: "MyTitle",
    src: "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg"
});

However, if you want to avoid duplicates, you'll have to iterate through the entire list to perform a comparison before adding the new row. Another solution could be to push everything then to remove duplicates only when required.

Comments

0

Try this out,

var new_obj = {};
new_obj.src = "/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg";
new_obj.title= "MyTitle";

carousel_images_info[carousel_images_info.length] = new_obj

So make a function if you are doing this dynamically,

carousel_images_info=[];

function Insert(xSrc,xTitle)
{
    var new_obj = {};
    new_obj.src = xSrc;
    new_obj.title= xTitle;

    carousel_images_info[carousel_images_info.length] = new_obj;
}

Insert("/_layouts/15/SSNIT.Portal/Images/ssnit02.jpg","MyTitle");

Comments

0

the value for the src and title is dynamic, so can i do like this?

  var item_enumerator = this.items.getEnumerator();
  while (item_enumerator.moveNext()) {
    var item = item_enumerator.get_current();
    var image_url = location.protocol + "//" + location.host + item.get_item('FileRef');
    var image_title = item.get_item('FileLeafRef'); 
    var set = {
      src: image_url,
      title : image_title
    };
    this.carousel_images_info.push(set);
  }

2 Comments

If the above is correct then it should be fine, but my carousel images does not work, probably because this.carousel_images_info is being accessed first by another function before it is populated.
getEnumerator - what's that? Seems the code is quite unrelated to the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.