0

I have an existing array which is like this

"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"
        }
    ]

I then want to create a new array which will be looking like this

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

I already have tried this by map() method

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

but it is not giving a proper array, please help me resolve this

0

4 Answers 4

5

Your map syntax is quite weird, I don't know what you tried to do, but that should be enough ;)

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Not weird, just incorrect here. foo = { [o]: v } is identical to foo = {}; foo[o] = v; it's called "computed property names". It is a recent addition to the language, very welcome, but it is just the wrong thing to do in this case.
Yeah, that definitely feels overcomplicated.
3

You could use a destructuring assignment with a object property assignment pattern [YDKJS: ES6 & Beyond] and map value with a short hand property.

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

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 Comments

why would you do a forEach on it? .map does this for you and was built for that
sorry... Im not used to use .map - working mostly on an old rhino engine which doesnt support .map
fair enough that is a way you can do it i guess
0

Something like this would work

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.