使用随机数组创建的 Map json 对象

Map json Object with randomise array creation

我有 JSON 个对象和数组

sampleJson={"id":1,"option1":"","option2":"","option3":""}
sampleArray=["car","","Animals"]

我想按以下格式将 sampleArray 添加到 sampleJson

sampleJson={"id":1,"option1"="car","option2":"Animals","option3":""}

样本数组将像 ["","car",""] Or ["","animal","Car"]?

一样随机化
var sampleJson={"id":1,"option1":"","option2":"","option3":""}
var sampleArray=["car","","Animals"];
var count = 1;
sampleArray.forEach(function(ele){
    if(ele.length){
        sampleJson['option' + count] = ele;
        count++;
    }
})
console.log(sampleJson);//{id: 1, option1: "car", option2: "Animals", option3: ""}
var sampleJson={"id":1,"option1":"","option2":"","option3":""}
var sampleArray=["car","","Animals"]
Object.keys(sampleJson).filter(function(k) {
    return k.includes('option');
}).forEach(function(k,i) {
    if (sampleArray.length > i) 
        sampleJson[k] = sampleArray[i];
});
console.log(sampleJson)

希望对您有所帮助!!

感谢您的帮助,我找到了解决方案

var i=1;
    sampleData.forEach(element => {
      if(element.length != 0){
        sampleJson['option'+i]=element;
        i++;
      }
    });