Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数组插入分组数组(JavaScript)_Javascript_Arrays - Fatal编程技术网

将数组插入分组数组(JavaScript)

将数组插入分组数组(JavaScript),javascript,arrays,Javascript,Arrays,我有一个分组数组 { Blues: [{title:"Blues", color:"red" },{title:"Blues",color:"white" },{title:"Blues",color:"white" }], Jazz: [{title:"Jazz", color:"orange"},{title:"Jazz",color:"pink"}] Rock: [{title:"Rock", color:"yellow"},{title:"Rock", color:"yellow"}

我有一个分组数组

{
Blues: [{title:"Blues", color:"red" },{title:"Blues",color:"white" },{title:"Blues",color:"white" }],
Jazz:  [{title:"Jazz", color:"orange"},{title:"Jazz",color:"pink"}]
Rock:  [{title:"Rock", color:"yellow"},{title:"Rock", color:"yellow"}]
}
然后我将有另一个数组{title:Rock,color:gold}有没有一种方法可以将该数组放入特定的分组数组中?如果它没有分组键,它将创建一个新的组,如{title:Metal,color:gold}


您没有要插入的数组,您有一个要插入到现有数组中的对象,或者如果密钥不存在,则使用该对象创建一个数组

function addOrCreate(value)
{

    // Get the keys that the object contains (e.g. "Blues", "Jazz" etc)
    var keys = Object.keys(arr);

    // Check if the title is in the keys array, 
    // if it isn't it'll return -1 otherwise it'll return the position of the element.
    if(keys.indexOf(value.title) > -1)
    {
       arr[value.title].push(value); // Add value to the existing array
    }
    else
    {
        arr[value.title] = [value]; // Create a new entry in the object with element wrapped as an array.
    }

}
使用.indexOf进行简单检查将确定该键是否在数组中。如果键在数组中,则推送到该数组,否则创建一个包含对象的新数组

var arr={ 蓝色:[{标题:蓝色,颜色:红色},{标题:蓝色,颜色:白色},{标题:蓝色,颜色:白色}], 爵士乐:[{标题:爵士乐,颜色:橙色},{标题:爵士乐,颜色:粉色}], 岩石:[{标题:岩石,颜色:黄色},{标题:岩石,颜色:黄色}] }; 函数addOrCreatevalue { var keys=Object.keysarr; ifkeys.indexOfvalue.title>-1 { arr[value.title].pushvalue; } 其他的 { arr[value.title]=[value]; } } addOrCreate{标题:岩石,颜色:黑色}; console.logarr; addOrCreate{title:123,color:black};
console.logarr;获取新项的title属性,然后检查大组中是否存在同名键提示:使用in运算符。如果它存在,只需将其添加到列表中,如果不只是执行group[title]=[newItem],请发布您为实现目标而尝试使用的实际JavaScript代码。P到目前为止,我所做的是获取原始数组,然后连接新对象,然后再次执行setState以使组var join=this.state.classDetailFromApi.concattobeAddlocalStore;this.setState{classDetailFromApi:joined,classDetailFromApi:u.groupBythis.state.classDetailFromApi,'title'}哇,太感谢了,伙计!,抱歉,如果我的解释有误,我是javascript新手,专门处理数组,因为到目前为止我所做的是获取原始数组,然后使用新对象将其合并,然后将再次分组,因为我很难将新对象插入到分组数组中,谢谢伙计!!你能帮我节省时间吗?如果你不介意的话,你能编辑你发布的代码并做一些注释和解释吗?呵呵@Adriani6@herotozero没问题。我已经在第一个片段中添加了注释。享受
function addOrCreate(value)
{

    // Get the keys that the object contains (e.g. "Blues", "Jazz" etc)
    var keys = Object.keys(arr);

    // Check if the title is in the keys array, 
    // if it isn't it'll return -1 otherwise it'll return the position of the element.
    if(keys.indexOf(value.title) > -1)
    {
       arr[value.title].push(value); // Add value to the existing array
    }
    else
    {
        arr[value.title] = [value]; // Create a new entry in the object with element wrapped as an array.
    }

}