Skip to main content
added 70 characters in body
Source Link
Nguyễn Văn Phong
  • 14.2k
  • 19
  • 47
  • 65

Solution 1: In case of the order of data and/or the different lengthIn case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {
            fruit, price, quality, 
            ...(index >= 0 && {count: b[index].count}) // the trick here
          };
});
console.log(result);

Solution 2:, If the 2 arrays make sure that the same length and order, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
//equivalent to: a.map((item, index) => ({...item, ...b[index]}));
console.log(result);

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {
            fruit, price, quality, 
            ...(index >= 0 && {count: b[index].count}) // the trick here
          };
});
console.log(result);

Solution 2:, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {
            fruit, price, quality, 
            ...(index >= 0 && {count: b[index].count}) // the trick here
          };
});
console.log(result);

Solution 2: If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
//equivalent to: a.map((item, index) => ({...item, ...b[index]}));
console.log(result);

added 70 characters in body
Source Link
Nguyễn Văn Phong
  • 14.2k
  • 19
  • 47
  • 65

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {
            fruit, price, quality, 
            ...(index >= 0 && {count: b[index].count}) // the trick here
          };
});
console.log(result);

Solution 2:, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {fruit, price, quality, ...(index >= 0 && {count: b[index].count})};
});
console.log(result);

Solution 2:, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {
            fruit, price, quality, 
            ...(index >= 0 && {count: b[index].count}) // the trick here
          };
});
console.log(result);

Solution 2:, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

added 85 characters in body
Source Link
Nguyễn Văn Phong
  • 14.2k
  • 19
  • 47
  • 65

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {fruit, price, quality, ...(index >= 0 && {count: b[index].count})};
});
console.log(result);

HoweverSolution 2:, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {fruit, price, quality, ...(index >= 0 && {count: b[index].count})};
});
console.log(result);

However, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

Solution 1: In case of the order of data and/or the different length

You can do the trick to render count property by using Conditionally add properties to an Object like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map(({fruit, price, quality}) => {
  const index = b.findIndex(r => r.fruit === fruit);
  return  {fruit, price, quality, ...(index >= 0 && {count: b[index].count})};
});
console.log(result);

Solution 2:, If the 2 arrays make sure that the same length and order, you can do simply like this

const a = [{fruit: 'banana', price: 100, quality:'high'},{fruit: 'orange', price:50, quality:'average'}];
const b = [{fruit: 'banana', count: 4},{fruit: 'orange', count: 10}];

const result = a.map((item, index) => Object.assign({}, item, b[index]));
console.log(result);

deleted 4 characters in body
Source Link
Nguyễn Văn Phong
  • 14.2k
  • 19
  • 47
  • 65
Loading
Source Link
Nguyễn Văn Phong
  • 14.2k
  • 19
  • 47
  • 65
Loading