-2

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

CodeBee
  • 31
  • 5
  • you can do this with array map. what is the final output needed – cmgchess Mar 30 '23 at 12:47
  • Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – pilchard Mar 30 '23 at 12:57

3 Answers3

0

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;
}
InSync
  • 4,851
  • 4
  • 8
  • 30
0

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);
imhvost
  • 4,750
  • 2
  • 8
  • 10
0

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));
Chris G
  • 1,598
  • 1
  • 6
  • 18
  • 1
    @pilchard yeah I forgot to move it inside the function, edited my asnwer – Chris G Mar 30 '23 at 13:15
  • `arr.flatMap(data => data.tags).map(ele => ele.name + ':' + ele.value);` avoid `forEach` when you have better options – Thomas Mar 30 '23 at 13:21