0

i have a meta tag that have structure like this

  metaInfo () {
    return {
      title: 'my title',
      meta: [
        {
          name: 'description',
          content: 'my description'
        },
        {
          property: 'og:title',
          content: 'my title2'
        },
        {
          property: 'og:site-name',
          content: 'my site name'
        },
        {
          property: 'og:type',
          content: 'website'
        },
        {
          name: 'robots',
          content: 'index,follow'
        }
      ]
    }

  },

and i want to append my api response to my meta tag, but i don't know how to make a data like this

this is my API response

data: [{meta_tags_id: 3, meta_tags_properties: "my property", meta_tags_content: "my content"}]
0: {meta_tags_id: 3, meta_tags_properties: "my property", meta_tags_content: "my content"}
meta_tags_content: "my content"
meta_tags_id: 3
meta_tags_properties: "my property"
error: 0
message: "successfully get all meta tags"

this is the expected result { property: my property, content: my content } and how do i append my json response to my metaInfo?

5
  • add the API response data also and would be great if you can show what is your desired result Commented Apr 22, 2021 at 14:15
  • done sir @decpk Commented Apr 22, 2021 at 14:17
  • Add api response as a text not as a picture and desired result as an object. It is not so clear what exactly do you want to do. Commented Apr 22, 2021 at 14:21
  • done sir, sorry @decpk Commented Apr 22, 2021 at 14:24
  • Answered, Is this what you want? Commented Apr 22, 2021 at 14:38

1 Answer 1

1

Since metaInfo is a function that returns an object, So collect that object in a container i.e metaInfoData.

map over your data array and convert it into required format and then append it into metaInfoData.meta

const metaInfo = function () {
  return {
    title: "my title",
    meta: [
      {
        name: "description",
        content: "my description",
      },
      {
        property: "og:title",
        content: "my title2",
      },
      {
        property: "og:site-name",
        content: "my site name",
      },
      {
        property: "og:type",
        content: "website",
      },
      {
        name: "robots",
        content: "index,follow",
      },
    ],
  };
};

const data = [
  {
    meta_tags_id: 3,
    meta_tags_properties: "my property",
    meta_tags_content: "my content",
  },
];

const metaInfoData = metaInfo();
const convertedData = data.map((obj) => {
  const { meta_tags_properties, meta_tags_content } = obj;
  return {
    property: meta_tags_properties,
    content: meta_tags_content,
  };
});
metaInfoData.meta = [...metaInfoData.meta, ...convertedData];
console.log(metaInfoData);

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

9 Comments

can you tell me how do i map those data to an array and convert it? sorry sir im tottaly new in javascript
See my code where I've used data.map((obj) => {.......
sir @decpk but how do i convert my response.data.data to const data =[] ?
const data = [...response.data.data] simple
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.