-2

Need to create a specific array of objects with the given objects. Now I have used object of assign.

Here is

var str = [{componentName: "Transformer", type: "PUB", beanName: "TransformerExtractor", state: "enabled", config: {"test":"test"}},{componentName: "DBEE", type: "PUBSUB", beanName: "DBEErExtractor", state: "enabled", config: {"test":"test"}}];

var A1 = [];

var B1 = [];

finalOutput = [];

for(i=0;i<str.length;i++){

    alert(str.length);

    var output = {};

    output.flowId = "Flow1234";

    output.flowAlias = "Flow1234";

    output.description = "Flow1234";

    output.state = "Enabled";

    A1.push(output);

    alert (A1);

    B1.push(str[i]);

    alert (B1);

    finalOutput = [A1, B1].map(o => Object.assign({}, A1, o));
}

alert (finalOutput);

return finalOutput;

Could you please tell me how do I get my expected result like [{A1,B1(from first set of variable str),{A1,B1(from 2nd set of variable str)]

Soma
  • 31
  • 6
  • Please create a minimal reproducible example, your code is far from minimal and that makes it hard to read. – Ruben Helsloot Jun 03 '19 at 11:00
  • 1
    [How to push object into array](https://stackoverflow.com/questions/40250139/push-object-into-array/40251425) – Krunal Limbad Jun 03 '19 at 11:00
  • Are you trying to achieve `A1+B1` and `A1+C1 ` ? – Sudhir Ojha Jun 03 '19 at 11:01
  • push-object is not working for getting my expected result – Soma Jun 03 '19 at 11:03
  • Okay i see, this might be helpful [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) – Krunal Limbad Jun 03 '19 at 11:08
  • see the answer by nina, it demonstrates the use of it. it will also overwrite the duplicate keys. – Krunal Limbad Jun 03 '19 at 11:09
  • Yes Kratos and using Nina's solution I am getiing like below {"str1":{"0":"componentName":"Transformation","type":"PUBSUB","beanName":"transformer","state":"enabled","config":"{\n\"area\": \"testVM\",\n\"pubTo\": \"trf_p\",\n\"subFrom\": \"db_p\",\n\"project\": \"testVM\",\n\"section\": \"FT\"\n}"},"1":"componentName":"testComponent","type":"PUB","beanName":"testComponent","state":"enabled","config":"{\"testComponent\":\"testComponent\"}"},"flowId":"ABC100","flowAlias":"","description":"","state":"enabled","creationDate":""}} – Soma Jun 03 '19 at 13:01
  • Now I want to avoid 0 and 1. Also the common section need to add to each of that part – Soma Jun 03 '19 at 13:02
  • Hi Kratos, I have edited my code by using Object Assign, yet still fighting to get desired data. Could you please help me. – Soma Jun 04 '19 at 12:02

2 Answers2

2

Taking the data from the last edit, you could define a template object for the data you like to add to a new object along to the objects of the array.

var data = [{ componentName: "Transformer", type: "PUB", beanName: "TransformerExtractor", state: "enabled", config: { test: "test" } },{ componentName: "DBEE", type: "PUBSUB", beanName: "DBEErExtractor", state: "enabled", config: { test: "test" } }],
    template = { flowId: "Flow1234", flowAlias: "Flow1234", description: "Flow1234", state: "Enabled" },
    result = data.map(o => Object.assign({}, template, o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanks Nina for your solution. Although using your idea I am getting like below {"str1":[{"0":"flowId":"ABC100","flowAlias":"","description":"","state":"enabled","creationDate":""},"1":"flowId":"ABC100","flowAlias":"","description":"","state":"enabled","creationDate":""}},{"0":"componentName":"Transformation","type":"PUBSUB","beanName":"transformer","state":"enabled","config":"{\n\"project\": \"testVM\",\n\"section\": \"FT\"\n}"},"1":"componentName":"testComponent","type":"PUB","beanName":"testComponent","state":"enabled","config":"{\"testComponent\":\"testComponent\"}"}}]} – Soma Jun 03 '19 at 11:40
  • it looks, you have different data, then posted. – Nina Scholz Jun 03 '19 at 11:46
  • Sorry again. Still I am fighting. I have edited my code here https://jsfiddle.net/e03onuxk/ – Soma Jun 03 '19 at 13:17
  • it's a complete different data set. please post what you have and what you want. – Nina Scholz Jun 03 '19 at 13:34
  • Thanks Nina. This is my code https://jsfiddle.net/e03onuxk/4/ and expected result will be like above. – Soma Jun 04 '19 at 04:31
  • you try to add data to an object which you already have. `A1` to `A1`, which is confusing. – Nina Scholz Jun 04 '19 at 07:20
  • According to my fiddle https://jsfiddle.net/e03onuxk/5/ I want to get {A1,One set of B1},{A1,another set of B1} and so on. – Soma Jun 04 '19 at 09:34
  • please ask a new question with raw data, your try and the wanted result. – Nina Scholz Jun 04 '19 at 09:54
  • Could you please give me a hint here since my ask question limit has been expired for this month :( – Soma Jun 04 '19 at 11:39
  • alternatively, you could append this question with the data, you have and the wanted result out of it. actually it's quite unclear, **why** you want to get a different result than posted from me and others. – Nina Scholz Jun 04 '19 at 11:41
  • Hi Nina, I have edited my code by using Object Assign, yet still fighting to get desired data. – Soma Jun 04 '19 at 12:03
  • you want to add `flowId`, `flow...`, and so on to every object of `str`? – Nina Scholz Jun 04 '19 at 12:04
  • Yesss Nina but I could not make it done :( – Soma Jun 04 '19 at 12:09
  • please have a look. – Nina Scholz Jun 04 '19 at 12:15
  • Thanks a lot Nina. This is working for me and today I have learnt a new in JS. Thanks again. – Soma Jun 04 '19 at 12:34
1

You could use lodash if you just want merge them

data = _.merge(A1, B1)
Anita
  • 476
  • 3
  • 10
  • 24