从中的现有数组创建一个新数组

时间:2018-07-11 07:22:46

标签: javascript reactjs react-native

我有一个像这样的现有数组

"activeSubject": [
        {
            "subject_id": "Class Teacher",
            "subject_name": "Class Teacher",
            "status": "2"
        },
        {
            "subject_id": "Academic Leader (Head)",
            "subject_name": "Academic Leader (Head)",
            "status": "2"
        },
        {
            "subject_id": "15",
            "subject_name": "Physics",
            "status": "2"
        }
    ]

然后我想创建一个新的数组

"new_subjects": [
        {
            "value": "Class Teacher",
        },
        {
            "value": "Academic Leader (Head)",
        },
        {
            "value": "Physics",
        }
    ]

我已经通过map()方法尝试过

var objA = this.state.activeSubject.map(o => ({
          [o]: { value: o.subject_name}
        }))

但它没有提供适当的数组,请帮助我解决此问题

4 个答案:

答案 0 :(得分:5)

您的map语法很奇怪,我不知道您想做什么,但这应该足够;)

this.state = {"activeSubject": [
    {
        "subject_id": "Class Teacher",
        "subject_name": "Class Teacher",
        "status": "2"
    },
    {
        "subject_id": "Academic Leader (Head)",
        "subject_name": "Academic Leader (Head)",
        "status": "2"
    },
    {
        "subject_id": "15",
        "subject_name": "Physics",
        "status": "2"
    }
]};

var objA = this.state.activeSubject.map(o => ({ value: o.subject_name}));
console.log(objA);

答案 1 :(得分:3)

您可以将destructuring assignmentobject property assignment pattern [YDKJS: ES6 & Beyond]一起使用,并映射console.log( $('#link1').data('test') ); //outputs "initvalue" $('#link1').data('test', 'newvalue'); console.log( $('#test').attr('data-test') ); //outputs "initvalue" as the attribute was never changed console.log( $('#test').data('test') ); //outputs "newvalue" as the value has been updated on the object short hand property

value
var array = [{ subject_id: "Class Teacher", subject_name: "Class Teacher", status: "2" }, { subject_id: "Academic Leader (Head)", subject_name: "Academic Leader (Head)", status: "2" }, { subject_id: "15", subject_name: "Physics", status: "2" }],
    result = array.map(({ subject_name: value }) => ({ value }));

console.log(result);

答案 2 :(得分:0)

var activeSubject = [
    {
        "subject_id": "Class Teacher",
        "subject_name": "Class Teacher",
        "status": "2"
    },
    {
        "subject_id": "Academic Leader (Head)",
        "subject_name": "Academic Leader (Head)",
        "status": "2"
    },
    {
        "subject_id": "15",
        "subject_name": "Physics",
        "status": "2"
    }
];
let newSubject = [];
activeSubject.forEach((obj) => { newSubject.push({ 'value:' obj.subject_name}); });
console.log(newSubject);

答案 3 :(得分:0)

类似的事情会起作用

this.state = {"activeSubject": [
    {
        "subject_id": "Class Teacher",
        "subject_name": "Class Teacher",
        "status": "2"
    },
    {
        "subject_id": "Academic Leader (Head)",
        "subject_name": "Academic Leader (Head)",
        "status": "2"
    },
    {
        "subject_id": "15",
        "subject_name": "Physics",
        "status": "2"
    }
]};

let objA = this.state.activeSubject.map(({ subject_name: value}) => ({ value }));

console.log(objA)