1

I have the following Array:

const arr = [{
    id: 0,
    title: 'A',
    countries: [{
      val: "1173",
      label: "England"
    }, {
      val: "1172",
      label: "Egypt"
    }],
    companies: [{
      val: "7346",
      label: "Ab Company"
    }]
  },
  {
    id: 1,
    title: 'B',
    countries: [{
      val: "1175",
      label: "France"
    }],
    companies: [{
      val: "8294",
      label: "Cd Company"
    }]
  },
]

What I want to achieve is:

const arr = [{
    id: 0,
    title: 'A',
    countries: ["England", "Egypt"],
    companies: ["Ab Company"]
  },
  {
    id: 1,
    title: 'B',
    countries: ["France"],
    companies: ["Cd Company"]
  },
]

My approach:

const mapJobArrValsToString = (arr) => {
  if (!(arr && arr.length)) {
    return [];
  }
  const fieldsToAddLabels = ['companies', 'countries'];
  const clonedArr = [...arr];

  clonedArr.forEach((job) => {
    const objKeysList = Object.keys(job).filter((fieldName) => fieldsToAddLabels.includes(fieldName));
    objKeysList.forEach((key) => {
      // eslint-disable-next-line no-param-reassign
      job[key] = job[key].map((el) => el.label);
    });
  });
  return clonedArr;
};

const arr = [{
    id: 0,
    title: 'A',
    countries: [{
      val: "1173",
      label: "England"
    }, {
      val: "1172",
      label: "Egypt"
    }],
    companies: [{
      val: "7346",
      label: "Ab Company"
    }]
  },
  {
    id: 1,
    title: 'B',
    countries: [{
      val: "1175",
      label: "France"
    }],
    companies: [{
      val: "8294",
      label: "Cd Company"
    }]
  },
];

console.log(mapJobArrValsToString(arr));

What am I doing wrong ?

2
  • 1
    your code works fine - and produces the expected result - why do you think it doesn't? Commented May 13, 2020 at 7:40
  • 1
    According to your own snippet, the code already works as expected, as far as I can see? Commented May 13, 2020 at 7:41

2 Answers 2

1

You could build new object and add this object to the copy of the object.

const
    array = [{ id: 0, title: 'A', countries: [{ val: "1173", label: "England" }, { val: "1172", label: "Egypt" }], companies: [{ val: "7346", label: "Ab Company" }] }, { id: 1, title: 'B', countries: [{ val: "1175", label: "France" }], companies: [{ val: "8294", label: "Cd Company" }] } ],
    keys = ['countries', 'companies'],
    result = array.map(o => ({
       ... o,
        ...Object.fromEntries(keys.map(k => [k, o[k].map(({ label }) => label)]))
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

You can do it this way too:

var arr = [{ id: 0, title: 'A', countries: [{ val: "1173", label: "England" }, { val: "1172", label: "Egypt" }], companies: [{ val: "7346", label: "Ab Company" }] }, { id: 1, title: 'B', countries: [{ val: "1175", label: "France" }], companies: [{ val: "8294", label: "Cd Company" }] }];
var keysFilter=['countries', 'companies'];

var result = arr.map((elem) => {
    for (const [key, value] of Object.entries(elem)) {
        if(keysFilter.includes(key)) elem[key] = value.map(val => val.label);
    }
    return elem;
});

console.log(result);

I hope this helps. Thanks! Happy Coding!

Comments

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.