Push items in objects from array when match - arrays

I have an array and object of items, I want to check each item in that array if its path has that object name, I push it in that object array.
So far this is all good, now if no match found I want to create a new item based on that array item name and push it inside it!
All my attempts ending in duplicated value, I think I need a third object/array I just can't think it anymore
To explain better:
cList = {
"rList": {
"Significant": [
{
"Path": "Significant\\Significant Charts",
"Name": "Charts"
}
]
},
};
and
SSList = {
value: [
{
"Name": "Test long name",
"Path": "/someFolder/Test long name",
},
{
"Name": "Untitled",
"Path": "/Significant/Untitled",
}
]
};
My current code
for (var cFolder in this.cList.rList) {
this.SSList.forEach((ssFile)=> {
if(ssFile.Path.indexOf(cFolder) >= 0){
this.cList.rList[cFolder].push(ssFile);
}
});
}
The first item in SSList will not be pushed since it doesn't match, I want to create a array and push it to inside rList
var folderName = ssFile.Path.split("/");
this.cList.rList[folderName[1]].push(ssFile);

One way to do it is to flip your inner and outer loops
let found = false;
this.SSList.value.forEach((ssFile) => {
for (var cFolder in this.cList.rList) {
if(ssFile.Path.indexOf(cFolder) >= 0){
found = true;
break;
}
}
if (found) {
this.cList.rList[cFolder].push(ssFile);
} else {
folderName = ssFile.Path.split("/");
if (!(folderName[1] in this.cList.rList))
this.cList.rList[folderName[1]] = [];
this.cList.rList[folderName[1]].push(ssFile);
}
found = false;
});

Related

Typescript - Querying or flattening nested array but keeping some objects as nested ones

Again I'm stuck with a nested Array of objects. I want to flatten it out, but I do have to keep some nested objects. The Problem I'm running into: How to rename the keys of the nested objects since I have an undefined number of nested objects. There might be 3 of them oder 8. So property1_3 has to be renamed to eg property1_3_1, property1_3_2 - depending on how many objects are in the original json data. And how to aply them to the correct parent object.
The json data I recieve looks like:
data = [{
"property1_1": "value1_1",
"property1_2": "value1_2",
"property1_3": [
[{
"subproperty1_1_1": "subvalue1_1_1",
"subproperty1_1_2": "subvalue1_1_2"
}],
[{
"subproperty1_2_1": "subvalue1_2_1",
"subproperty1_2_2": "subvalue1_2_2"
}]
]
},
{
"property2_1": "value2_1",
"property2_2": "value2_2",
"property2_3": [
[{
"subproperty2_1_1": "subvalue2_2_1",
"subproperty2_1_2": "subvalue2_2_2"
}],
[{
"subproperty2_2_1": "subvalue2_2_1",
"subproperty2_2_2": "subvalue2_2_2"
}],
[{
"subproperty2_3_1": "subvalue2_2_1",
"subproperty2_3_2": "subvalue2_2_2"
}]
]
}
]
What I want to achieve now is:
data = [
{
"property1_1": "value1_1",
"property1_2": "value1_2",
"property1_3_index1": {"subproperty1_1_1":"subvalue1_1_1", "subproperty1_1_2":"subvalue1_1_2"},
"property1_3_index2": {"subproperty1_2_1":"subvalue1_2_1", "subproperty1_2_2":"subvalue1_2_2"}
},
{
"property2_1": "value2_1",
"property2_2": "value2_2",
"property2_3_index1": {"subproperty2_1_1":"subvalue2_2_1", "subproperty2_1_2":"subvalue2_2_2"},
"property2_3_index2": {"subproperty2_2_1":"subvalue2_2_1", "subproperty2_2_2":"subvalue2_2_2"},
"property2_3_index3": {"subproperty2_3_1":"subvalue2_2_1", "subproperty2_3_2":"subvalue2_2_2"}
}
]
My last try was:
transformData(input) {
const testArray = [];
input.map(obj => {
for (const prop in obj) {
if (obj.hasOwnProperty(prop) && Array.isArray(obj[prop])) {
for (const [index, element] of obj[prop].entries()) {
testArray.push(element[0]);
}
}
}
});
}
but this only leeds to an array with all the single subobjects in one array. I'm also not quite sure if it's best trying to convert the original data or to build a new array as I tried before.
I finally found a way to achieve this.
transformData(input) {
return input.map(obj => {
for (const prop in obj) {
if (obj.hasOwnProperty(prop) && Array.isArray(obj[prop])) {
for (let i = 0; i < obj[prop].length; i++) {
const name = prop + (i + 1).toString();
obj[name] = obj[prop].flat(1)[i];
}
delete obj[prop];
}
}
return obj;
});
}

Make a list from a multidimensional aray

I have an array of values, which I would like to put in an UL. To be specific some thing in an order like this :
< Li with the IDs > One more obj/ information from the arrray
Is there a way to import an image from an array, and how can I import the array to a list?
<div>
<ul id=cakeList>
</ul>
</div>
let cakes = [
{
"ID":"Strawberry" ,
"Name": "Vanilla Strawberry Cake",
"Eggs":false,
"Time":45,
"IMG":"https://cdn.shortpixel.ai/spai/q_glossy+ret_img+to_webp/https://omgchocolatedesserts.com/wp-content/uploads/2016/03/Strawberry-Shortcake-2.jpg" },
{
"ID": "choco1" ,
"Name": "Chocolate cake",
"Eggs":true,
"Time":120,
"IMG":"https://www.bbcgoodfood.com/sites/default/files/styles/recipe/public/recipe_images/recipe-image-legacy-id--1043451_11.jpg?itok=Z_w2WOYB", },
{
"ID": "cheesecake1" ,
"Name": "Strawberry cheesecake",
"Eggs":false,
"Time":30,
"IMG":"https://www.bbcgoodfood.com/sites/default/files/recipe-collections/collection-image/2013/05/recipe-image-legacy-id-1028453_10.jpg" },
];
function createList() {
var list = document.createElement('ul');
for (var i = 0; i < cakes.length; i++) {
for(var j=0, len2=cakes[i].length; j<len2; j++){
var item = document.createElement('li');
item.appendChild(document.createTextNode(cakes[i][j]));
list.appendChild(item);
}
return list;
}
}
document.getElementById('cakeList').appendChild(createList(cakes[i][1]));
https://jsfiddle.net/t0b3y0u/q9gck36o/37/
The first thing is that that array is not multidimensional but a simple array of objects. So what you need to do is first access each object itself and then the property you want to display:
function createList() {
var list = document.createElement('ul');
cakes.forEach(function(cake){
var item = document.createElement('li');
item.appendChild(document.createTextNode(cake.ID));
list.appendChild(item);
})
return list
}
document.getElementById('cakeList').appendChild(createList());
```

Adding paragraph text input by loop [TypeScript]

I have this variable in my TypeScript :
data = {
"paragraphs": [
{ "paragraph": "" }
]
}
When the screen is loaded, I want to add the { "paragraph": "" } in the array to become like as follow.
data = {
"paragraphs": [
{ "paragraph": "" },
{ "paragraph": "" },
{ "paragraph": "" }
]
}
So what I did was, I make a for loop to keep adding the { "paragraph": "" } in the array but it's not completed yet.
let dataParag = 3; // The int value will keep changing based on data from database
for (var i = this.data.paragraphs.length; i <= dataParag; i++) {
this.data.paragraphs = [
{ "paragraph": "" } // The paragraph will be keep added in here but I don't have any idea on how to add it
];
}
Is there any good way to add it dynamically?
Oh nevermind as I found the solution for this. This is how I do the thing :
let dataParag = 3;
for (var i = 1; i < dataParag; i++) {
this.data.paragraphs.push({ "paragraph": "" });
}

convert JSON data into table based on elements seperated by \n for header elements and \t for table elements using angularJS

"results": {
"code": "SUCCESS",
"msg": [
{
"type": "TABLE",
"data": "id\tfirstname\tlastname\n1\tJack\tSmith\n2\tAdam\tJohnson\n3\tKim\tSmith\n4\tDavid\tWilliams\n5\tPeter\tDavis\n6\tJack\tSmith\n7\tAdam\tJohnson\n8\tKim\tSmith\n9\tDavid\tWilliams\n10\tPeter\tDavis\n11\tPeter\n31\tJack\tSmith\n32\tAdam\tJohnson\n33\tKim\tSmith\n34\tDavid\tWilliams\n35\tPeter\tDavis\n"
}
]
},
this is my example code,where ever we have \n consider as header elements of table and \t consider as tr elements,can you provide me any suggestion.
You might be looking something like this. In your HTML code right one ng repeat for head and other for each row of the table.
var result = {
"code": "SUCCESS",
"msg": [{
"type": "TABLE",
"data": "id\tfirstname\tlastname\n1\tJack\tSmith\n2\tAdam\tJohnson\n3\tKim\tSmith\n4\tDavid\tWilliams\n5\tPeter\tDavis\n6\tJack\tSmith\n7\tAdam\tJohnson\n8\tKim\tSmith\n9\tDavid\tWilliams\n10\tPeter\tDavis\n11\tPeter\n31\tJack\tSmith\n32\tAdam\tJohnson\n33\tKim\tSmith\n34\tDavid\tWilliams\n35\tPeter\tDavis\n"
}]
}
var format = result.msg.map((r) => {
var str = r.data;
var arr = str.split('\n');
var head = arr.splice(0, 1)[0].split('\t');
arr.pop(); // remove empty string
var data = arr.map((d) => {
return d.split('\t').reduce((obj, x, index) => {
obj[head[index]] = x;
return obj;
}, {})
})
return {
head,
data
}
});
console.log(format);

How dynamically transform my "Object" to List in ng-model at view

I'm trying to transform my object to list dynamically, so I'm building at view instead of declaring at controller.
I don't want to declare like this: custom_fields.title_field.type_text_field = [] because the title_field is built dynamic, it could be any kind of text like full_name
My json as is:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":{
"name":"John",
"first_name":"Wick"
},
"type_boolean_field":{
"is_badass": true,
"is_good_movie": true
},
"type_select_field": {
"this_select": 1,
"i_got_this": "nope i didnt got this"
}
},
And to be:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":[{
"name":"John",
"first_name":"Wick"
}],
"type_boolean_field":[{
"is_badass": true,
"is_good_movie": true
}],
"type_select_field": [{
"this_select": 1,
"i_got_this": "nope i didnt got this"
}]
},
the object I'm trying to transform into array is type_text_field which can be dynamic too, like type_date_field or type_select_field and so on.
My ng-model is like this:
ng-model="objectApp.application.applicant.custom_fields[layout.dynamic_title][input.type][input.variable]"
the [input.type] is that I'm trying to transform into array, how can I achieve this? I tried to use $index, but got strange results.
We can do it by 2 solutions:
There is a question about your task:
? how you want handle if we have more than one type_text_field in title_dynamic_generate_field? because you want to convert it to "type_text_field":[{},...]
however my answers about the question are:
If we know what's the dynamic params which we want to send theme as json, i mean if we know what is the key of title_dynamic_generate_field or type_text_field, we do as this sample:
var data = {
"custom_fields": {
dynamicParamIs1: 'title_dynamic_generate_field',
dynamicParamIs2: 'type_text_field',
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var paramHelper1 = json.custom_fields[json.custom_fields.dynamicParamIs1];
var paramHelper2 = json.custom_fields.dynamicParamIs2;
var solutionA = function (object, as) {
var array = [];
for (var key in object) {
var newObject = object[key];
array.push(newObject);
}
object[as] = array;
}
solutionA(paramHelper1, paramHelper2);
We changed a model of our json which can help us to detect (find) the keys
If we don't know what is the dynamic params are, we do as this:
var data = {
"custom_fields": {
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var solutionB = function (json) {
var array = [];
for (var key in json) {
var j1 = json[key];
for (var key2 in j1) {
var j2 = j1[key2];
for (var key3 in j2) {
var fullObject = j2[key3];
array.push(fullObject);
j2[key3] = array;
}
}
}
}
solutionB(data);
This sample is manual which we use nested for to detect the keys name

Resources