skip to Main Content

I want to map this response data coming from Woocommerce Rest API.

This is a snippet of the original Woocommerce Rest API response:

{
    number: "206"
    discount_tax: "0.00"
    discount_total: "380.00"
    meta_data {
        0 {
            id: 2928
            key: "is_vat_exempt"
            value: "no"
        }
        1 {
            id: 2929
            key: "att_info-94_1"
            value: "||CJ||Cortez||  ||||84||"
        }
        2 {
            id: 2930
            key: "_alternate_phone"
            value: "98765432"
        }
    }
}

And I want to transform it like this one.

{
    number: "206"
    discount_tax: "0.00"
    discount_total: "380.00"
    meta_data {
        is_vat_exempt: "no"
        att_info-94_1: "||CJ||Cortez||  ||||84||"
        _alternate_phone: "98765432"
    }
}

My Code:

public getFlightOrder() {
    var params = {status:'completed',};
    this.data_api.getflightOrder(params,this.passID.id)
        .pipe(
            map(responseData => {})
         )
        .subscribe((data) => {
            this.bookingData = data;
        }
    );
}

As you can see there is no mapping yet as I don’t know where to start.

2

Answers


  1. This question doesn’t really have anything to do with Angular 8 specifically but you can do something like this

    const data = {
        number: "206",
        discount_tax: "0.00",
        discount_total: "380.00",
        meta_data: [
            {
                id: 2928,
                key: "is_vat_exempt",
                value: "no"
            },
            {
                id: 2929,
                key: "att_info-94_1",
                value: "||CJ||Cortez||  ||||84||"
            },
            {
                id: 2930,
                key: "_alternate_phone",
                value: "98765432"
            }
        ]
    }
    
    data.meta_data = Object.assign({}, ...data.meta_data.map(x => ({[x.key]: x.value})));
    
    console.log(data);

    EDIT

    As per your question you can do the following

    public getFlightOrder() {
      var params = {status:'completed',};
      this.data_api.getflightOrder(params,this.passID.id)
        .subscribe((data) => {
          this.bookingData = {
            ...data, meta_data: Object.assign({}, ...data.meta_data.map(x => ({[x.key]: x.value})))
          };
        });
    }
    
    Login or Signup to reply.
  2. Try something like this:

    const data = {
        number: "206",
        discount_tax: "0.00",
        discount_total: "380.00",
        meta_data: [
            {
                id: 2928,
                key: "is_vat_exempt",
                value: "no"
            },
            {
                id: 2929,
                key: "att_info-94_1",
                value: "||CJ||Cortez||  ||||84||"
            },
            {
                id: 2930,
                key: "_alternate_phone",
                value: "98765432"
            }
        ]
    }
    
    const newData = {
      ...data,
      meta_data: data.meta_data.map((item) => ({[item.key]: item.value}))
    };
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search