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

Kedar Dave
  • 471
  • 12
  • 25

4 Answers4

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);
sjahan
  • 5,720
  • 3
  • 19
  • 42
  • 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. – Amadan Jul 11 '18 at 07:31
  • Yeah, that definitely feels overcomplicated. – sjahan Jul 11 '18 at 07:32
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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
Kapsonfire
  • 1,013
  • 5
  • 17
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)
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22