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,我正在javascript a中创建一个基于一些数组数据的对象 var myArr = [ { "id": 1, "Music": "POP", "Singer": "Abc", "Country": "IND" }, { "id": 2, "Music": "JAZZ", "Singer": "xyz", "Country": "USA"

我正在javascript a中创建一个基于一些数组数据的对象

var myArr = [
    {
        "id": 1,
        "Music": "POP",
        "Singer": "Abc",
        "Country": "IND"
    },
    {
        "id": 2,
        "Music": "JAZZ",
        "Singer": "xyz",
        "Country": "USA"
    },
    {
        "id": 3,
        "Music": "BLUES",
        "Singer": "def",
        "Country": "ENG"
    }
]
现在,基于这个数组,我试图创建一个数组对象,它具有

id、值和国家。

这就是我要尝试的

var myObj = [];

for (var i = 0; i < myArr.length; i++) {
    var obj = {
        id: i,
        Value: myArr[i].Music,
        myArr[i].Singer : myArr[i].Country,
    }
    myObj.push(obj);
}
var myObj=[];
对于(变量i=0;i

但是在这个for循环中,
myArr[i].Singer
抛出了一个错误。如何解决这个问题

使用数组,获取想要的键,并直接在对象文本之外赋值

var myArr=[{id:1,音乐:“流行”,歌手:“Abc”,国家:“IND”},{id:2,音乐:“爵士”,歌手:“xyz”,国家:“美国”},{id:3,音乐:“布鲁斯”,歌手:“def”,国家:“ENG”},
myObj=[],
obj;
对于(变量i=0;i
。作为控制台包装{max height:100%!important;top:0;}
您可以使用
array#map

var myArr=[{“id”:1,“音乐”:“流行”,“歌手”:“Abc”,“国家”:“IND”},{“id”:2,“音乐”:“爵士乐”,“歌手”:“xyz”,“国家”:“美国”},{“id”:3,“音乐”:“布鲁斯”,“歌手”:“def”,“国家”:“ENG”};
var result=myArr.map({Music,Singer,Country},i)=>({id:i,Value:Music,[Singer]:Country});
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
这里有一点
ES6/ES7
。使用现有对象数组中所需的道具创建新的对象数组

const modifiedArray = myArr.map( item => {
    //This will return only the elements from the object you want
    const picked = (({
        width,
        height,
        type,
        size,
        name
    }) => ({
        width,
        height,
        type,
        size,
        name
    }))(item);
    // Return the object and insert it in the array
    return {
        ...picked,
    };
});
您可以使用:

var myArr=[{id:1,“音乐”:“流行”、“歌手”:“Abc”、“国家”:“IND”},{id:2,“音乐”:“爵士”、“歌手”:“xyz”、“国家”:“美国”},{id:3,“音乐”:“布鲁斯”、“歌手”:“def”、“国家”:“ENG”},
myObj=myArr.map(函数(元素,索引){
返回{
id:索引,
价值:元素音乐,
[元素歌手]:元素乡村
};
});

console.log(myObj)所以它是Java,不是Javascript?你的myArr对象看起来不像Javascript。这是一个混乱-
myArr
是一个对象,不是数组,甚至不是一个有效的对象,语法是错误的
myObj
实际上是一个数组。我真的不知道如何回应-你能展示一下你的代码中的
myArr
是什么吗?因为现在,这只会抛出snytax错误。@Walk我更新了我的问题。谢谢你指出,你能给出你期望的结果吗?
const modifiedArray = myArr.map( item => {
    //This will return only the elements from the object you want
    const picked = (({
        width,
        height,
        type,
        size,
        name
    }) => ({
        width,
        height,
        type,
        size,
        name
    }))(item);
    // Return the object and insert it in the array
    return {
        ...picked,
    };
});