简体   繁体   English

动态将数据添加到JSON

[英]dynamic adding of data to JSON

So what I'm trying to do is storing the dataUri of pictures in my localstorage. 所以我想做的是将图片的dataUri存储在我的本地存储中。 I am only using javascript and angularJS. 我只使用javascript和angularJS。 No jQuery at this point. 目前没有jQuery。

The pictures are uploaded through inputs of type="file" with ids like " input_file_0 " and so on. 图片是通过类型为“ file”的输入(其ID为“ input_file_0 ”等)上传的。

I am able to store the id and the dataUri in my $scope.files variable. 我可以将id和dataUri存储在$ scope.files变量中。 So that if I say console.log($scope.files); 因此,如果我说console.log($scope.files); I get an output like this: 我得到这样的输出:

$scope.files $ scope.files

< [input_file_0:File, input_file_1:File, ...]
  length:0
  < input_file_0:File
       lastModified: 1457515898145
       lastModifiedDate: Wed Mar 09 2016 10:31:38 GMT+0100 
       name: "name-of-picture.png"
       size: 206156
       src:  "data:image/png;base64,iVBORw0K..."
       type: "image/png"

  > input_file_1:File

controller.js controller.js

/*
 * stores $scope-values for longer reference in the localStorage
 * @returns {undefined}
 */
    $scope.storeDataInLocalStorage = function () {
        if (typeof (localStorage) !== 'undefined') {
            //storing file data
            var data = {};
            for (var i = 0; i <= $scope.files.length; i++) {
                data.push({ 'file_input_'+i : $scope.files['file_input_'+i].src}) ;
            }
            localStorage.setItem('files', JSON.stringify(data));
        }
        /* No localstorage support */
        else {
            alert("Sorry. Localstorage is not supported");
        }
    };

I want to achieve a localStorage item like this: 我想实现这样的localStorage项:

localStorage.getItem('files') = 
{'input_file_0' : "data:image/png;base64,iVBORw0K...", 
 'input_file_1 : "data:image/png;base64,iVBORw0K..."}

But setting the key just doesn't work. 但是设置密钥只是行不通的。 Getting the src-attribute works just fine. 获得src属性就可以了。 But having a variable to be the key doesn't work. 但是用变量作为键是行不通的。 It always substitutes it with just the variablename. 它总是只用变量名代替它。


I solved it myself trying something I tried before that didn't work and now suddenly does. 我自己尝试解决了之前无法解决的问题,现在突然解决了。 Maybe there was something already stored locally and i didnt' delete it correctly. 也许已经在本地存储了一些东西,但我没有正确删除它。

I didn't use push this time and so my code looks like this: 这次我没有使用push,所以我的代码如下所示:

controller.js controller.js

/*
 * stores $scope-values for longer reference in the localStorage
 * @returns {undefined}
 */
    $scope.storeDataInLocalStorage = function () {
        if (typeof (localStorage) !== 'undefined') {
            //storing file data
            var data = {};
            for (var i = 0; i <= $scope.files.length; i++) {

                data['input_file_'+i]= $scope.data['input_file_'+i].src;

            }
            localStorage.setItem('files', JSON.stringify(data));
        }
        /* No localstorage support */
        else {
            alert("Sorry. Localstorage is not supported");
        }
    };

still anyone an idea how to do it using array.push() ? 还是有人知道如何使用array.push()吗?

Use extra object to add items in for loop: 使用额外的对象在for循环中添加项目:

  var data = [];
  for (var i = 0; i <= $scope.files.length; i++) {
        var o = {};
        o[('file_input_' + i)] = $scope.files['file_input_'+i].src;
        data.push(o);
   }
  localStorage.setItem('files', JSON.stringify(data));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM