从给定对象中获取Array-Object-String

时间:2018-10-02 11:36:34

标签: javascript arrays string object

我有这样的对象
freq = { a: 50, r: 25, m: 25 }

我想将其转换为 Array-Object 这样的东西

dps = [  
   { label: "a",
     y: 50  },
   { label: "r",
     y: 25  },
   { label: "m",
     y: 25  }
];

这是用于创建带有画布的图表。

4 个答案:

答案 0 :(得分:4)

您可以获取对象的条目,并为键/值对获取destructuring assignment,并使用short hand properties映射新对象。

var freq = { a: 50, r: 25, m: 25 },
    dps = Object.entries(freq).map(([label, y]) => ({ label, y }));
    
console.log(dps);

答案 1 :(得分:2)

尝试一下,我认为这是最高效的方法,因为version: '3' services: app: container_name: app build: . volumes: - .:/var/project working_dir: /var/project ports: - "8080:80" depends_on: - mysql - phpmyadmin composer: restart: 'no' image: composer:latest command: install --ignore-platform-reqs volumes: - .:/var/project working_dir: /var/project node: restart: 'no' image: node:8 command: npm install volumes: - .:/var/project working_dir: /var/project mysql: image: mysql:5.7 container_name: mysql network_mode: services:app environment: MYSQL_DATABASE: app_db MYSQL_USER: app MYSQL_PASSWORD: app MYSQL_RANDOM_ROOT_PASSWORD: "true" phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin network_mode: services:app restart: always environment: PMA_HOST: mysql PMA_PORT: 3306 PMA_USER: app PMA_PASSWORD: app 比简单的.map()循环慢:

for

答案 2 :(得分:0)

您可以使用关注方法:

    function b64toBlob(b64Data, contentType, sliceSize) {
        contentType = contentType || '';
        sliceSize = sliceSize || 512;

        var byteCharacters = atob(b64Data);
        var byteArrays = [];

        for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            var slice = byteCharacters.slice(offset, offset + sliceSize);

            var byteNumbers = new Array(slice.length);
            for (var i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }

            var byteArray = new Uint8Array(byteNumbers);

            byteArrays.push(byteArray);
        }

      var blob = new Blob(byteArrays, {type: contentType});
      return blob;


    function reloadPage(){
       window.location.reload();
    }

    function appendFileAndSubmit(getf){
        var form = document.getElementById("up-img");

        var ImageURL = getf;
        var block = ImageURL.split(";");
        var contentType = block[0].split(":")[1];
        var realData = block[1].split(",")[1];
        var blob = b64toBlob(realData, contentType);
        var fd = new FormData(form);
        fd.append("img", blob);

        $.ajax({
            url:"/upload-img.php",
            data: fd,
            type:"POST",
            contentType:false,
            processData:false,
            cache:false,
            dataType:"json",
            error:function(err){
                //
            },
            success:function(data){
                //
            },
            complete:function(){
                reloadPage();
            }
        });
    }

    document.onpaste = function(event){
        var items = (event.clipboardData || event.originalEvent.clipboardData).items;
        console.log(JSON.stringify(items));
        for (index in items) {
            var item = items[index];
            if (item.kind === 'file') {
                var blob = item.getAsFile();
                var reader = new FileReader();
                reader.onload = function(event) {
                    appendFileAndSubmit(event.target.result);
                };
                reader.readAsDataURL(blob);
            }
        }
    } 

答案 3 :(得分:-1)

使用循环和地图

//Using for loop for object interation
const freq = { a: 50, r: 25, m: 25 };
var finalFreq=[];
for(let i in freq){
  finalFreq.push({label:i,y:freq[i]});
}
console.log(finalFreq);

//Using map and object keys
var finalFreq=Object.keys(freq).map((freqIndex)=>{
              return {label:freqIndex,y:freq[freqIndex]};
              });
console.log(finalFreq);