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)">
Related
I have a nested value in array and I want to update value using useState and it is updated but it also create new index value in array which is not required.
let data = {
query: [
{
com: "",
id: 1,
rules: [{ fN: "tej", sN: "raj" }]
},
{}
]
};
here I want to update value of fN only but new index [1] created in query at same time. here is link to code - https://codesandbox.io/s/blissful-night-kkycxh?file=/src/App.js:103-239
I would do as below, using a state update function, as you are trying just with index 0 it doesn't really matter
useEffect(() => {
setQuery((prev)=>{
return prev.map((q,idx)=>{
if(idx === 0){
q.rules[0].fN = "ram"
}
return q
})
});
}, []);
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.
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 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}}})
I have a document in MongoDB as below.
{
"CorePrice" : 1,
"_id" : 166,
"partno" : 76,
"parttype" : "qpnm",
"shipping" :
[
{
"shippingMethod1" : "ground",
"cost1" : "10"
},
{
"shippingMethod2" : "air",
"cost2" : "11"
},
{
"shippingMethod3" : "USPS",
"cost3" : "3"
},
{
"shippingMethod4" : "USPS",
"cost4" : 45
}
]
}
My goal is to add CorePrice (1) to cost4 (45) and retrieve the computed value as a new column "dpv". I tried using the below query. However I receive an error exception: $add only supports numeric or date types, not Array. I'm not sure why. Any kind of help will be greatly appreciated.
db.Parts.aggregate([
{
$project: {
partno: 1,
parttype: 1,
dpv: {$add: ["$CorePrice","$shipping.cost1"]}
}
},
{
$match: {"_id":{$lt:5}}
}
]);
When you refer to the field shipping.cost1 and shipping is an array, MongoDB does not know which entry of the shipping-array you are referring to. In your case there is only one entry in the array with a field cost1, but this can't be guaranteed. That's why you get an error.
When you are able to change your database schema, I would recommend you to turn shipping into an object with a field for each shipping-type. This would allow you to address these better. When this is impossible or would break some other use-case, you could try to access the array entry by numeric index (shipping.0.cost1).
Another thing you could try is to use the $sum-operator to create the sum of all shipping.cost1 fields. When there is only one element in the array with a field cost1, the result will be its value.
I am able to achieve this by divorcing the query into two as below.
var pipeline1 = [
{
"$unwind": "$shipping"
},
{
$project:{
partno:1,
parttype:1,
dpv:{
$add:["$CorePrice","$shipping.cost4"]
}
}
},
{
$match:{"_id":5}
}
];
R = db.tb.aggregate( pipeline );