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.
Related
I have a data called votes, i want to get value votes.voteDict.12
But 12 is a variable, that's a cat id get from api. And i want shop.poiId = 691904
How can i call it?
<v-if="shop.poiId === votes.voteDict.[catid]">
data: function () {
return {
votes:[]
}
}
Data
"voteDict": {
"12": [
691904,
649950,
649031
],
"13": [
677267,
686968
],
"29": [
545344,
499690,
431970,
618904,
510121
],
"30": [
618540,
613464,
632345,
672805,
654174
],
Thank you
I would like to know how to get this data
You will need the value 691904 stored in some variable depending on some condition as 12 contains "12": [691904,649950,649031]. Then you can try:
computed: {
selectedIndex() {
const idx = votes.voteDict[catid].findIndex(num => num === 691904)
return idx === -1 ? 0 : idx
}
}
Then change your if statement to
<v-if="shop.poiId === votes.voteDict[catid][selectedIndex]">
Again, the number (691904) in the computed property should be a variable set according to some condition or dependent on the result of an API call.
You should use Array.includes to test for the shop.poiId value. Also accessing "12" property of votes.voteDict would be like votes.voteDict[catId] where catId is the integer 12
<div v-if="votes.voteDict[catId].includes(shop.poiId)">
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.
Let's say I'm searching this JSON array for the car model "Focus"
and I want to return its position in the array.
I think indexOf is returning a result of -1 because I'm not stopping the loop. I'm a beginner coder.
The reason I want to return its position is so I can return other results related to "Focus" such as "Ford" by simply knowing its position in the array.
var myObj, i, j, x = "";
myObj = {
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
for (i = 0; i < myObj.cars.length; i++) {
for (j in myObj.cars[i].models) {
x = myObj.cars[i].models[j];
a = x.indexOf("Focus");
document.getElementById("demo").innerHTML = a;
}
}
<p id="demo"></p>
Rather than deal with two different for-loops, use one and try to get the index of whatever each iteration. Test the value of x for -1 (meaning not found). If it isn't -1, that means it was found, and x is the index.
for (i = 0; i < myObj.cars.length; i++) {
x = myObj.cars[i].models.indexOf("Focus");
if (-1 != x) {
document.getElementById("demo").innerHTML = x;
break;
}
}
The break breaks the for-loop so in the case of collision you'll just get the first valid index.
Edit
As a side-note I thought I'd mention what your problem really was: Your indexOf() call is returning a value, but you aren't checking it for -1 values. In your original code, if you tested that a != -1 it would only confirm a == "Focus" and not give you any details on its index. In the end, for a minimal edit, you would need to do
if (-1 != a) {
document.getElementById("demo").innerHTML = j;
}
which is equivalent to
if ("Focus" == x) {
...
}
First i would parse the json with JSON.parse method.
Attention: I am using ECMAScript 6 (ES6) Feature here to use multiline strings. This is not supported by older browsers.
Then i would make use of the Array Prototype functions like map() and find() to first map the index of all models with 'Focus' to a new array and then take the first where the index is 0 or higher. indexOf() Returns -1 when the element your looking for does not exist.
var json = `{
"name": "John",
"age": 30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}`
var myObj = JSON.parse(json)
var focusIndex = myObj.cars.map(c => c.models.indexOf('Focus')).find(i => i >= 0)
document.getElementById("demo").innerHTML = focusIndex
<p id="demo"></p>
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.
I am learning node and now I'm trying to order an array like this:
"lng" : [{
"ES" : 5,
"EN" : 3,
"IT" : 4
}]
(This is a part of a query result in MongoDB), I need to order the array by the number:
"ES" : 5,
"IT" : 4,
"EN" : 3
I used sort() but this function orders the array alphabetically by the first parameter, but I need order by the second, I've tried a lot of things but without result.
Thank you for your help!
JavaScript has no ordered objects, so first you should transform your object to an array of this kind:
[
{ key: "ES", value: 5 },
{ key: "EN", value: 3 },
{ key: "IT", value: 4 }
]
And then sort by the value key.
You can easily do it as follows:
// ("mongoDbResult" is the variable with an object you get from MongoDB)
var result = mongoDbResult.lng;
result = Object.keys(result).map(function (key) {
return { key: key, value: result[key] };
});
And then just sort by the value key:
result.sort(function (a, b) {
return (a.value < b.value) ? -1 : 1;
});
As a result, you should get a sorted array in the result variable.
Thank you Nikita, adding the key and value the sort works perfectly, my problem now is make the query to get the results with specific key and value...
I can get the elements by the KEY:
db.users.find({"lng.key" : "ES"})
Or by the VALUE:
db.users.find({"lng.value" : 5})
But not the both at the same query :/
[EDIT]
I have find the solution, $elemMatch:
db.users.find({lng_array : {$elemMatch:{key:"ES", value:5}}})