skip to Main Content

I have object:

const data3 = [
  {
    calculatedPropertyDescription: "where evri_courionsignature_gb is Dispatched",
    category: "red",
    tags: [
    { name: 'LandingPage', value: 'true' },
    { name: 'Country', value: 'GB' },
    { name: 'Carrier', value: 'RoyalMail' },
    { name: 'EventCode', value: 'Dispatched' },
    ]
 },
 {
   calculatedPropertyDescription: "where evgnature_gb is Dispatched",
   category: "red",
   tags: [
     { name: 'LandingPage', value: 'true' },
     { name: 'Country', value: 'USA' },
     { name: 'Carrier', value: 'Evri' },
     { name: 'EventCode', value: 'Dispatched' },
   ]
 },
 ]

I need from tags: create array of pairs values.
For exmple from:

{ name: 'LandingPage', value: 'true' },

I need get:

[..., 'LandingPage:true', 'Country:GB',...]

Thank you

3

Answers


  1. A simple Array#map() will do:

    function mergeKeyValuePairs(tags) {
      return tags.map(({ name, value }) => `${name}:${value}`);
    }
    

    Explanation: This means "convert each tag object in a list of tags to a string which is concatenated from the tag’s name, a colon and value". .map() means to do something for every element in a given array.

    Try it:

    const data3 = [
      {
        calculatedPropertyDescription: "where evri_courionsignature_gb is Dispatched",
        category: "red",
        tags: [
          { name: 'LandingPage', value: 'true' },
          { name: 'Country', value: 'GB' },
          { name: 'Carrier', value: 'RoyalMail' },
          { name: 'EventCode', value: 'Dispatched' },
        ]
     },
     {
       calculatedPropertyDescription: "where evgnature_gb is Dispatched",
       category: "red",
       tags: [
         { name: 'LandingPage', value: 'true' },
         { name: 'Country', value: 'USA' },
         { name: 'Carrier', value: 'Evri' },
         { name: 'EventCode', value: 'Dispatched' },
       ]
     },
    ];
    
    function mergeKeyValuePairs(tags) {
      return tags.map(({ name, value }) => `${name}:${value}`);
    }
    
    console.log(data3);
    data3.forEach(
      object => object.tags = mergeKeyValuePairs(object.tags)
    );
    console.log(data3);
    .as-console-wrapper {
      max-height: 100% !important;
    }
    Login or Signup to reply.
  2. I will try to assume that there should be one array at the output:

    const data3 = [
      {
        calculatedPropertyDescription: "where evri_courionsignature_gb is Dispatched",
        category: "red",
        tags: [
        { name: 'LandingPage', value: 'true' },
        { name: 'Country', value: 'GB' },
        { name: 'Carrier', value: 'RoyalMail' },
        { name: 'EventCode', value: 'Dispatched' },
        ]
     },
     {
       calculatedPropertyDescription: "where evgnature_gb is Dispatched",
       category: "red",
       tags: [
         { name: 'LandingPage', value: 'true' },
         { name: 'Country', value: 'USA' },
         { name: 'Carrier', value: 'Evri' },
         { name: 'EventCode', value: 'Dispatched' },
       ]
     },
    ]
     
    const result = data3.reduce((previousValue, currentValue, index, array) => {
      currentValue.tags.forEach(el => previousValue.push(el.name + ':' + el.value));
      return previousValue;
    }, []);
    console.log(result);

    If you need to separate results:

    const data3 = [
      {
        calculatedPropertyDescription: "where evri_courionsignature_gb is Dispatched",
        category: "red",
        tags: [
        { name: 'LandingPage', value: 'true' },
        { name: 'Country', value: 'GB' },
        { name: 'Carrier', value: 'RoyalMail' },
        { name: 'EventCode', value: 'Dispatched' },
        ]
     },
     {
       calculatedPropertyDescription: "where evgnature_gb is Dispatched",
       category: "red",
       tags: [
         { name: 'LandingPage', value: 'true' },
         { name: 'Country', value: 'USA' },
         { name: 'Carrier', value: 'Evri' },
         { name: 'EventCode', value: 'Dispatched' },
       ]
     },
    ]
     
    const result = data3.reduce((previousValue, currentValue, index, array) => {
      previousValue[index] = [];
      currentValue.tags.forEach(el => previousValue[index].push(el.name + ':' + el.value));
      return previousValue;
    }, []);
    console.log(result);
    Login or Signup to reply.
  3. Assumming that you want everything on the tags in that format, in one array then you can do it like this with forEach

    const data3 = [
      {
        calculatedPropertyDescription: "where evri_courionsignature_gb is Dispatched",
        category: "red",
        tags: [
        { name: 'LandingPage', value: 'true' },
        { name: 'Country', value: 'GB' },
        { name: 'Carrier', value: 'RoyalMail' },
        { name: 'EventCode', value: 'Dispatched' },
        ]
     },
     {
       calculatedPropertyDescription: "where evgnature_gb is Dispatched",
       category: "red",
       tags: [
         { name: 'LandingPage', value: 'true' },
         { name: 'Country', value: 'USA' },
         { name: 'Carrier', value: 'Evri' },
         { name: 'EventCode', value: 'Dispatched' },
       ]
     },
    ]
    
    
    function concat(arr){
        let result = [];
        arr.forEach(data => {
          data.tags.forEach(ele => {
            result.push(ele.name+':'+ele.value);
        })
      })
      return result;
    }
    
    
    console.log(concat(data3));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search