S
S
Sergey questions2018-02-08 22:37:14
JavaScript
Sergey questions, 2018-02-08 22:37:14

Why doesn't the javascript array work?

Hello.
The problem is this, the array does not work when it contains text names.

For example, this code works:

var variants = {
    '66': {'images_names':[4984534-1_1.jpg,4984534-2_1.jpg], 'v_id':[66]},
    '208': {'images_names':[4984534-3_1.jpg,4984534-4_1.jpg], 'v_id':[208]},
  };
  
  function changeVariant(){
    var variant = $('input[name=variant]:checked'),
    val = variants[variant.val()],
    images_names = val['images_names'],
    v_id = val['v_id'],
    image_id = images_names[0],
    arr = [];
  }


Swears at
'66': {'images_names':[4984534-1_1.jpg,4984534-2_1.jpg], 'v_id':[66]},
and on But if you work with image IDs, namely var variants , replace withimages_names = val['images_names'],


var variants = {
    '66': {'images_ids':[168,156], 'v_id':[66], 'percent':'20'},
    '208': {'images_ids':[158,159], 'v_id':[208], 'percent':'84'},
  };


That all works perfectly. Help who is strong in JS. The task is to work with the names of the photos, and not with their IDs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-08
@smartycms

Pass filenames as strings:

var variants = {
  '66': {'images_names':['4984534-1_1.jpg', '4984534-2_1.jpg'], 'v_id':[66]},
  '208': {'images_names':['4984534-3_1.jpg', '4984534-4_1.jpg'], 'v_id':[208]},
 };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question