Why is my array.find and array.filter returning empty - arrays

I have an Array that looks like this
resultList: any[];
this.resultList = [
{
"id": "14e6f101-f8e8-4914-930f-cbba7a9c5328",
"value": "Dream Inc."
},
{
"id": "9a16c1c0-68f3-498a-b968-61849ccc2a21",
"value": "Dressing, Inc. ABC LOREBOX SERVICES"
},
{
"id": "919b3cdb-c5fb-40c8-b88f-6c7f44f8446f",
"value": "Dresson-Brand Company"
}
]
I am having a hard time trying to retrieve the second item (index 1) by using the value. I can get the other two items by value but the second item returns undefined
code I am using
search(value: any) {
//value coming in
//Dressing, Inc. ABC LOREBOX SERVICES
console.log('*** value to search ***', value);
var foundObj = this.resultList.find(p => p.value == value);
console.log('*** foundObj ***', foundObj);
}
foundObj comes back empty and same results when I do a filter. Any idea's what is wrong with that string that it can't be found? It seems I can get any other string (this is a dynamic array) but this one.

The issue came from the dataList I was using. In the element I was missing the 'value' attribute. Once I added that the spaces were no longer removed and my filtering was able to pick it up.

Related

Eliminate zeroed values in an array with dataweave function

In this array of a list of devices, you would need to delete from the array when any item was zeroed out
[
{
"valueTotal": "6.50"
},
{
"bread": "001",
"value": "3.00"
},
{
"milk": "002",
"value": "3.50"
},
{
"coffe": "003",
"value": "0.00"
}
]
Assuming you intend to remove the items in the input, whose "value" field is 0 and then get the totalValue. Here is a quick one I have come up with(could be improved).
%dw 2.0
output application/json
//filter the items whose value is zero
var filteredPayload= ((payload [-1 to 1] map (item1, index1) ->
{
(if (item1.value as Number != 0) (item1) else null)
}) filter ($ != {}))
// get the totalValue from the filteredPayload
var totalFilteredPayload = filteredPayload reduce ($.value + $$.value)
---
// simply add both the arrays
filteredPayload ++ [{ "valueTotal": totalFilteredPayload as String }]
If by "zeroed out" you mean value = 0 it is just a basic filter operation
payload filter ($.valueTotal? or ($.value as Number != 0))
The condition $.valueTotal? is to make the object with valueTotal pass the check. And the other one is for the value itself.

Resolving simple references in a spreadsheet structure. Is this a linked list problem?

I have this job test task where I am need some help.
I have to somehow dynamically access the value in the data stucture below, through references. To this point I used loops and indexOf() in an ordered alphabet and numbers. indexOf("B") in alphabet is [[ 1 ]], indexOf(1) is [0] for the two dimensional array.
Here "B1" is referencing "C1", "C1" references "D1" .. to "H1" and when "H1" points to the text "Last", all of the formulas should turn into the same value - the text "Last".
But how? Been breaking my head for a couple of hours and remembered linked lists, but so far have no clue how to implement them. How can I achieve this?
{
data: [
[
{ reference: "B1" },
{ reference: "C1" },
{ reference: "D1" },
{ reference: "E1" },
{ reference: "F1" },
{ reference: "G1" },
{ reference: "H1" },
{ text: "Last" },
]
],
id: "job-20"
}
It looks like you need to work with a spreadsheet type of data structure, and need to recalculate cells based on the formulas they have.
You can use recursion for that.
The base case happens when you arrive at a cell that has no reference. In that case the text of that cell can be returned to the caller. And that caller will assign that text to the cell it was looking at, and also returns that to their caller...etc.
function get(reference) { // return the cell object at the given reference
return data[reference.slice(1)-1][reference.charCodeAt() - "A".charCodeAt()];
}
function resolve(cell, visited=new Set) {
// Have we found a cell with just text? Then return that to caller
if (!("reference" in cell)) return cell.text;
// Did we already meet this cell before? Then we are running in circles!
if (visited.has(cell)) return "Circular Reference!";
// Make a recursive call to know what the text should be
cell.text = resolve(get(cell.reference), visited.add(cell));
delete cell.reference;
return cell.text; // Return this value also to the caller
}
function calculate(data) {
// Get to each cell object...
for (let row of data) {
for (let cell of row) {
resolve(cell); // And update its text property by following references
}
}
}
let data = [
[
{reference: "B1"}, {reference: "C1"}, {reference: "D1"}, {reference: "E1"},
{reference: "F1"}, {reference: "G1"}, {reference: "H1"}, {text: "Last"},
]
];
calculate(data);
console.log(data);
It should be noted that in a real spreadsheet the references (formulas) are not deleted during such a recalculation. The cell will have both the text and the reference, so that when a cell's text is updated, all other dependent cells get their own text updated accordingly again.

UI5: Why is the formatter called a second time with a 'null' value?

I'm using a XML-Fragment defining a ComboBox where I want to add some core:CustomData with a value returned by a formatter. The formatter is called the first time when the fragment shows up and a second time when I open the dropdown list of the combobox. First time I get a single entry(from entries) from the model as expected. At the second call the parameter is null.
What am I doing wrong?
'myModel' is a static json model loaded via manifest.json:
{
"entries": [
{
"title": "first entry",
"hide": false,
"id": 1
},
{
"title": "second entry",
"hide": true,
"id": 2
}
]
}
The ComboBox defined inside a FragmentDefinition inside a Popover:
<ComboBox id="xys" selectedKey="{myModel>/aKey}" value="{myModel>/aValue}"
items="{myModel>/entries}">
<core:ListItem key="{myModel>id}" text="{path: 'myModel>', formatter: '.entryTextFormatter'}" >
<core:customData>
<core:CustomData key="hide-item" value="{path: 'myModel>', formatter: '.hideItemFormatter'}" writeToDom="true"/>
</core:customData>
</core:ListItem>
</ComboBox>
And the formatter code looks like:
/* Test cases */
entryTextFormatter: function(entry) {
return entry.title;
},
hideItemFormatter: function(entry) {
if(entry && (entry.hide == true)) {
return "true";
} else {
return "false";
}
},
Edit: Note! If using the same formatter for the CustomData and the ListItem it is only called twice for the CustomData.
Does anyone has an idea why the formatter gets null as parameter the second time?

Converting a typescript class object with dictionary to a JSON array

After some digging I decided my backend needed to consume duplicate keys and as a consequence my frontend can no longer send a dictionary as a JSON string.
See my previous question.
After applying the solution provided
let mediatagRequest = new MediaTagRequest(tags);
const headers = { 'content-type': 'application/json' }
let jsonObject = {};
for (let entry of mediatagRequest.tags.entries())
{
jsonObject[entry[0]] = entry[1];
}
const body = JSON.stringify({
tags: jsonObject
});
My current output (which is what I then wanted)
{
"tags": {
"city": "Karachi"
}
However my needs have changed and after a bit of of struggle I couldn't get my desired output to be like this
{
"tags": [
{
"key": "city",
"value": "Karachi"
},
{
"key": "city",
"value": "Mumbai"
}
]
}
Could someone help, thank you.
To get your desired output you could use the Object.entries() function to get the key, value pairs separately. This code segment will turn an object into a list of objects with key value pairs:
test_object = {
karachi: "dubai",
mumbao: "moscow",
};
output = Object.entries(test_object).map(([key, value]) => ({ key, value}));
console.log(output);
You can adapt this code to select the desired parts of your object and format them as you like. There are other Object functions you can see in the documentation.

Typescript filter array of object by another array of strings

i stuck at my typescript filter function.
I have an array of objects:
[
{
"id": 12345,
"title": "Some title",
"complexity": [
{
"slug": "1" // my search term
"name": "easy"
}, {
"slug": "2" // my search term
"name": "middle"
},
{...}
And i have a array of strings with the allowed complexity:
public allowedComplexityArray:Array<string> = ["1"];
My Task: I only want to show objects with the allowed complexity of "1".
But somehow my function dont work and i dont know why:
allowedMeals = meals.filter(meal => {
return meal.complexity.every(complexityObj => that.allowedComplexityArray.indexOf(complexityObj.slug) > -1)
});
try:
let allowedMeals = data.filter(meal => {
return meal.complexity.findIndex(complexityObj =>
allowedComplexityArray.findIndex(m => m == complexityObj.slug) > -1) > -1
});
I'm using findIndex instead of filter in the return clause so it does not need to scan the whole array every time.

Resources