I have following JSON structure:
var pages = {
"yearly": {
"MXN": {
"id" : "2c92c0f940f1b6e50140f4b7d9054a6e",
},
"NZD": {
"id" : "2c92c0f940f1b6d40140f4b7e14d66b7",
}
},
"monthly": {
"MXN": {
"id" : "2c92c0f940f1b6d40140f4b40ed85c57",
},
"NZD": {
"id" : "2c92c0f840f1c2cc0140f4b3b15d3956",
}
}
}
i want to get the values of json inside yearly property dynamically for example i get the dynamic value MXN inside a variable currency now i am having problem in accessing its value i am trying to access it like this:
pages.yearly[currency].id
but i am unable to get to that id property gives me error of TypeError: pages.monthly[currency] is undefined what is wrong in my syntax that is causing type error??
The according to this error the variable currency needs to be defined. For example
var currency = "MXN";
Related
I am trying to update a document in Mongo if it has a certain value within a field.
{
"_id" : ObjectId("myObject"),
"source" : "BW",
"sourceTableName" : "myTable",
"tableName" : "tier",
"type" : "main",
"fieldMappings" : [
{
"sourceField" : "tier_id", <~~~~ If this is "tier_id", I want to change it to "trmls_id"
"reportingField" : "bw_id",
"type" : "integer",
"defaultMap" : {
"shouldErrorOnConvert" : false
}
}
]
}
I tried something like
db.getCollection('entityMap').update({"sourceTableName":"myTable"}, {"fieldMappings.0.sourceField":"trmls_id":{$exists : true}}, { $set: { "fieldMappings.0.sourceField": "tier_id" } })
and I think it is failing on the $exists parameter I am setting.
Is there a more cleaner/dynamic way to do this?
The whole query I am trying to formulate is
In the table myTable
I want to check if there is a sourceField that has the value tier_id
If there is tier_id, then change it to trmls_id
Otherwise do nothing
If there is a similar StackOverflow question that answers this, I am happy to explore it! Thanks!
There is a syntax mistake and you can use positional operator $ in update part because you have already selector field in query part,
db.getCollection('entityMap').update(
{
"sourceTableName": "myTable",
"fieldMappings.sourceField": "tier_id" // if this field found then it will go to update part
},
{
$set: {
"fieldMappings.$.sourceField": "trmls_id"
}
}
)
I have an array of objects like
[
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
}
]
But when trying to access the object property 'id' using console.log(array[0].id) throws a "cannot read property id of undefined error"
However just logging the first object with console.log(array[0]) prints the object successfully.
{id: 17368, creationDate: 1566802693000, …}
Also printing the list of ids using array.map(x => console.log(x.id)) prints the list of ids successfully .
I am in a situation where i need to access the first few specifically . Where am i going wrong ?
try this console.log(array[0] && array[0].id)
or you can use get from lodash-es like this:
import { get } from 'lodash-es'
const id=get(array[0], 'id', '')
My code is like this :
<script>
export default {
props:['search','category','shop'],
created(){
...
},
data(){
return{
loading:false
}
},
computed:{
...
list:function(){
console.log(this.$store.state.product);
return this.$store.state.product.list
},
},
methods:{
...
}
}
</script>
I do : console.log(this.$store.state.product); in list method
Then, I check it on the console
The result is like this :
I want display value of name
I try like this :
console.log(this.$store.state.product.list.id.name);
There exist error :
Uncaught TypeError: Cannot read property 'name' of undefined
How can I solve the error?
The list is an object, with the keys being the different element id's - you are currently trying to access like this:
this.$store.state.product.list.id.name
You are getting that error because there is no id key in the list object, you need to replace .id with the actual id value, like this:
this.$store.state.product.list["12"].name; //"Bunga Gandeng"
process.env.ENVIRONMENT = dev2
Input JSON:
{
"base": {
"product1" : "dev1.awesomeproduct1.com",
"product2" : "dev1.awesomeproduct2.com"
}
}
Output JSON:
Based on the process.env.ENVIRONMENT the product urls should be updated dynamically
{
"base": {
"product1" : "dev2.awesomeproduct1.com",
"product2" : "dev2.awesomeproduct2.com"
}
}
Do I correctly understand that you want to replace the part of the domain names up to the dot with your process.env.ENVIRONMENT variable?
Then the following code should work:
for (key in myJSON.base) {
myJSON.base[key] = myJSON.base[key].replace(/^[^.]+/, process.env.ENVIRONMENT);
}
Obviously, you will need to amend it if there are other fields than product<n> in the base object, or if you need to do a more complex replacement.
I have the following JSON.
{
"lang": [
{
"SECTION_NAME": {
"english": "My title"
},
"SECTION_NAME_2": {
"english": "My title"
}
}
]
}
And I'm looking to print the value like this:
$.getJSON('json/lang.json', function(data) {
var text = data['lang']['SECTION_NAME'];
$('#title').html(text.english);
});
But I have the following error:
TypeError: undefined is not an object (evaluating 'text.english')
Any help please.
Thanks.
You have to access it via index as lang is an array of object
like this
console.log(data['lang'][0]['SECTION_NAME'])
JSFIDDLE
The value of lang is an array which contains an object.
You are ignoring the array and trying to access the objects as if it was the value of lang directly.