0

I want to insert duplicate values into array based on length provided like this:

var a = [{displayName: 'bar'}, {displayName:'google'}, {displayName:'mod'}];;
var dataTypesLength= 4;

Output should be like:

var a = [{displayName: 'bar'},{displayName: 'bar'},{displayName: 'bar'},{displayName: 'bar'}, {displayName:'google'},{displayName:'google'},{displayName:'google'},{displayName:'google'}, {displayName:'mod'}, {displayName:'mod'}, {displayName:'mod'}, {displayName:'mod'}];

I tried this:

a = a.flatMap( word => Array.from({ dataTypesLength}).fill( word ));

but I am getting typescript error saying: Argument of type '{ dataTypesLength: any; }' is not assignable to parameter of type 'ArrayLike<{}>'. Object literal may only specify known properties, and 'dataTypesLength' does not exist in type 'ArrayLike<{}>'

3
  • 1
    Start with something simple and something you can cope with: eg with 2 nested loops, one iterates the a, the other iterates 1..4 and pushes items from a. This is a straightforward and naive solution. As long as you have it working - you can see if you can turn it into composition of functions. Commented Jun 12, 2019 at 4:22
  • That object is not an array like - it has a dataTypesLength property, not a length property... Commented Jun 12, 2019 at 4:24
  • 1
    @zerkms it's also a solution OP knows about Commented Jun 12, 2019 at 4:25

4 Answers 4

2

Just loop the array of objects and use fill to get your result.

var a = [{displayName: 'bar'}, {displayName:'google'}, {displayName:'mod'}];
var dataTypesLength= 4;
var arr=[];
function fillArray(value, len) {
  return Array(len).fill(value);
}
for (var i=0; i<a.length; i++){
  var b = fillArray(a[i],dataTypesLength);
  arr.push(...b);
}
console.log(arr);

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

Comments

2

You can use this (single line solution):

a.reduce((acc,x)=>{ acc.push(...new Array(dataTypesLength).fill(x)); return acc;}, [])

Explaination :

  • Reducing the input array starting from empty array []
  • For every element creating a new array with length dataTypesLength and filling all the new array with the current element new Array(dataTypesLength).fill(x)
  • Spread the new created array and push it into accumulator array acc.push(...new Array(dataTypesLength).fill(x))
  • Return the accumulator array

Comments

2

You can use map with Object.assign so that each object will have independent reference

var a = [{displayName: 'bar'}, {displayName:'google'}, {displayName:'mod'}];
var dataTypesLength= 4;

a= a.map(obj => { return Array(dataTypesLength).fill(null).map( e => Object.assign({}, obj)) }).flat();
console.log(a)

2 Comments

one small thing. each set of objects are in different arrays. I think OP needs in same array
We can use Array.flat(). Updated the answer
0

you can try reduce

var a = [{displayName: 'bar'}, {displayName:'google'}, {displayName:'mod'}];;
var dataTypesLength= 4;

a  = a.reduce(
(f,obj ) => {
var dup = new Array(dataTypesLength); 
f.push(...dup.fill(obj));
return f;
}, [] )

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.