I found a nice example on here showing how to look through arrayObjects with a condition but I have a question.
As is stands its console.logging every time it the condition is false. Is it possible to only console.log once when its finished looping through everything.
var arrayObjects = [{
"building": "A",
"status": "good"
},
{
"building": "B",
"status": "horrible"
}
];
for (var i = 0; i < arrayObjects.length; i++) {
console.log(arrayObjects[i]);
for (key in arrayObjects[i]) {
if (key == "status" && arrayObjects[i][key] == "good") {
console.log(key + "->" + arrayObjects[i][key]);
} else {
console.log("nothing found");
}
}
}
Simply use .length with if condition.
var arrayObjects = [{
"building": "A",
"status": "good"
},
{
"building": "B",
"status": "horrible"
}
];
for (var i = 0; i < arrayObjects.length; i++) {
console.log(arrayObjects[i]);
if( i === arrayObjects.length-1 ) {
console.log("nothing found");
}
}
I'm assuming that you want it to print Nothing found when nothing's really found, not even a single thing..
Then, you can try this.
var arrayObjects = [{"building":"A", "status":"good"},
{"building":"B","status":"horrible"}];
var isFound = false;
for (var i=0; i< arrayObjects.length; i++) {
console.log(arrayObjects[i]);
for(key in arrayObjects[i]) {
if (key == "status" && arrayObjects[i][key] == "good") {
isFound = true
console.log(key + "->" + arrayObjects[i][key]);
}
}
}
if (isFound === false){
console.log("nothing found");
}
You can use the some or filter method of array.
var arrayObjects = [{"building":"A", "status":"good"},
{"building":"B","status":"horrible"}];
const found = arrayObjects.some(it => it.status === 'good')
if (found) {
console.log('found')
}
const items = arrayObjects.filter(it => it.status === 'good')
if (items.length) {
console.log('found')
}
If you're willing to refactor you code, you can save on time complexity by using just one loop with Array.reduce()
var arrayObjects = [{
"building": "A",
"status": "good"
},
{
"building": "B",
"status": "horrible"
}
];
const foundKeys = arrayObjects.reduce((bool, key) => {
console.log(key)
if (key.status === "good") {
console.log("status ->", key.status);
bool = true
}
return bool
}, false)
if (!foundKeys) {
console.log("Nothing found")
}
Another declarative way solution:
const arrayObjects = [
{ "building": "A", "status": "good" },
{ "building": "B", "status": "horrible" },
];
const checkCondition = (arr, key = 'status', value ='good') => {
const result = arr.find((obj) => obj[key] === value);
return result
? `${key} -> ${result[key]}`
: "nothing found";
};
console.log(checkCondition(arrayObjects)); //status -> good
console.log(checkCondition(arrayObjects, 'building', 'B')); //building -> B
console.log(checkCondition(arrayObjects, 'building', 'C')); //nothing found
Related
Given the following object:
const ourObject = {
"payload": {
"streams": [
{
"children": {
"2165d20a-6276-468f-a02f-1abd65cad618": {
"additionalInformation": {
"narrative": {
"apple": "A",
"banana": "B"
},
"myInventory": {
"fruits": [
{
"name": "apple"
},
{
"name": "banana"
}
]
}
}
}
}
}
]
}
};
We're trying to find the path of myInventory, the issue is that the children's uuid will be different each time. Any idea how we can get the path to myInventory by providing it as a key and get the json path to it?
If things are dynamic, a programmatic key search could help
const ourObject = {
"payload": {
"streams": [
{
"children": {
"2165d20a-6276-468f-a02f-1abd65cad618": {
"additionalInformation": {
"narrative": {
"apple": "A",
"banana": "B"
},
"myInventory": {
"fruits": [
{
"name": "apple"
},
{
"name": "banana"
}
]
}
}
}
}
}
]
}
};
const getPath = (key, o) => {
if (!o || typeof o !== "object") {
return "";
}
const keys = Object.keys(o);
for(let i = 0; i < keys.length; i++) {
if (keys[i] === key ) {
return key;
}
const path = getPath(key, o[keys[i]]);
if (path) {
return keys[i] + "." + path;
}
}
return "";
};
const getValueForKey = (key, o) => {
if (!o || typeof o !== "object") {
return undefined;
}
const keys = Object.keys(o);
for(let i = 0; i < keys.length; i++) {
if (keys[i] === key ) {
return o[key];
}
const value = getValueForKey(key, o[keys[i]]);
if (value) {
return value;
}
}
return undefined;
}
console.log(getPath("myInventory", ourObject))
console.log(getValueForKey("myInventory", ourObject))
Not sure if I understand the question right but
let uuid = '2165d20a-6276-468f-a02f-1abd65cad618';
ourObject.payload.streams[0].children[uuid].additionalInformation.myInventory
var changingKey = Object.keys(ourObject["payload"]["streams"][0]["children"])[0];
console.log(ourObject["payload"]["streams"][0]["children"][changingKey]["additionalInformation"]["myInventory"]);
Okay, you could create a helper function that gets the UUID. Since it's an object, the lookup is close to O(1) especially given the case that the children has only one key-value pair here.
function getUUIDFromPayload(payload) {
let obj = payload.streams[0].children
let uuid = Object.keys(obj)[0]
return uuid
}
Usage
const uuid = getUUIDFromPayload(payload)
ourObject.payload.streams[0].children[uuid].additionalInformation.myInventory
I have an json array as below.If I find atleast one object with status as Active I need to set response =true or else false.I tried below but didnt worked .can someone help.
arr=[{"id":"1","status":"active"},{"id":"2","status":"complete"},
{"id":"3","status":"complete"}]
for (var i = 0; i < arr.len; i++) {
if (arr[i].status == "active") {
response = true;
} else {
response = false;
}
}
console.log(response);
Use Array#some to check if at least one element got status property with active value.
var arr = [{"id":"1","status":"active"},{"id":"2","status":"complete"},
{"id":"3","status":"complete"}],
response = arr.some(v => v.status == 'active');
console.log(response);
You set your response to true, don't break the loop and then set it to false again when you meet another value.
You may simply set initial condition and then break when you find an item. You can actually even not break when you find an item, but it is useless to continue iteration when already found an item.
var arr = [{
"id": "1",
"status": "active"
}, {
"id": "2",
"status": "complete"
},
{
"id": "3",
"status": "complete"
}
];
var response = false;
for (var i = 0; i < arr.length; i++) {
if (arr[i].status == "active") {
response = true;
break;
}
}
console.log(response);
Another elegant way is to use function and make a short-circuit return:
var arr = [{
"id": "1",
"status": "active"
}, {
"id": "2",
"status": "complete"
},
{
"id": "3",
"status": "complete"
}
];
function hasActiveStatus(a) {
for (var i = 0; i < a.length; i++) {
if (a[i].status == "active") {
return true;
}
}
return false;
}
var response = hasActiveStatus(arr);
console.log(response);
Note that you had .len for some reason, I have replaced it with correct .length.
Even more elegant way is to use Array.prototype.some:
var arr = [{
"id": "1",
"status": "active"
}, {
"id": "2",
"status": "complete"
},
{
"id": "3",
"status": "complete"
}
];
var response = arr.some(function(x) { return x.status === "active"; });
console.log(response);
var response = false;
var arr=[{"id":"1","status":"active"},{"id":"2","status":"complete"},
{"id":"3","status":"complete"}]
for (var i = 0; i < arr.length; i++) {
if (arr[i].status == "active") {
response = true;
break;
}
}
console.log(response);
I have a JSON with the following structure:
{
"root": {
"containers": [
{
"id": UNIQUE_ID,
...
"child": [
{
"id": UNIQUE_ID,
...
"child": [...]
}
]
},
{
"id": UNIQUE_ID,
...
"child": [...]
}
]
}
}
root.containers and root.containers.child have the same structure. The thing is that I can have infinite nesting and I don't know beforehand what is the total number of child nodes, since they are added to this JSON dynamically.
I need a function that returns me only the specific object with the given ID as parameter. So it has to dive into the JSON until it finds the child with that ID. I've tried something with .filters but I can't figure out how to search deeper. Probably some search algorithm that I've never implemented in javascript before...
Can someone give me an idea how can I accomplish this? Thanks!
The function that you need is:
function findById(data, id){
var found;
data.forEach(function(o){
if(found){
return;
}
found = o.id === id && o || o.child && findById(o.child, id);
});
return found;
}
And it would be used in this way:
findById(data.root.containers, 1)
Check and run the following snippet. it has some tests, including one case to fail.
var data = {
"root": {
"containers": [
{
"id": 1,
"child": [
{
"id": 2,
"child": [{
id: 3
}, {
id: 4
}]
}
]
},
{
"id": 5,
"child": [{
id: 6
}]
},
{
"id": 7,
"child": []
}
]
}
};
function findById(data, id){
var found;
data.forEach(function(o){
if(found){
return;
}
found = o.id === id && o || o.child && findById(o.child, id);
});
return found;
}
[1,2,3,4,5,6,7,8].forEach(function(v){
console.log('==== Searching for:', v);
console.log(findById(data.root.containers, v));
});
You can use a recursive function like this (https://jsfiddle.net/17qLjufc/):
//this is just a function to check for null or undefined
var notEmpty = function(something){
if(typeof(something) !== 'undefined' && something !== null){
return true;
}
return false;
}
//this is a recursive function that does the search in dept indefinetly (supposing that all the nodes from the containers node on just have child properties)
var findNodeById = function (node, id){
if(notEmpty(node) && notEmpty(node.id) && node.id == id)
return node;
else{
if(notEmpty(node) && notEmpty(node.child)){
for (var i = 0 ; i < node.child.length; i++){
var found = findNodeById(node.child[i], id);
if(found != null)
return found;
}
}
}
return null;
}
//this is going through the containers children and call the search for each of them until the first is found.
var searchById = function(root, id){
var found;
if(notEmpty(root) && notEmpty(root.containers)){
for(var i = 0; i < root.containers.length; i++){
found = findNodeById(root.containers[i], id);
if(found !== null){
break;
}
}
}
return found;
}
I have this json file:
var data = [{
"id": 0,
"parentId": null,
"name": "Comapny",
"children": [
{
"id": 1235,
"parentId": 0,
"name": "Experiences",
"children": [
{
"id": 3333,
"parentId": 154,
"name": "Lifestyle",
"children": []
},
{
"id": 319291392,
"parentId": 318767104,
"name": "Other Experiences",
"children": []
}
]
}
]
}];
I need to find object by id. For example if need to find an object with id:319291392, I have to get:
{"id": 319291392,"parentId": 318767104,"name": "Other Experiences","children": []}
How can I do that?
I tried to use this function:
function findId(obj, id) {
if (obj.id == id) {
return obj;
}
if (obj.children) {
for (var i = 0; i < obj.children.length; i++) {
var found = findId(obj.children[i], id);
if (found) {
return found;
}
}
}
return false;
}
But it doesn't work as it's an array of objects.
If your starting point is an array, you want to invert your logic a bit, starting with the array rather than with the object:
function findId(array, id) {
var i, found, obj;
for (i = 0; i < array.length; ++i) {
obj = array[i];
if (obj.id == id) {
return obj;
}
if (obj.children) {
found = findId(obj.children, id);
if (found) {
return found;
}
}
}
return false; // <= You might consider null or undefined here
}
Then
var result = findId(data, 319291392);
...finds the object with id 319291392.
Live Example
This should work for you:-
var serachById = function (id,data) {
for (var i = 0; i < data.length; i++) {
if(id==data[i].id)
return data[i];
if(data[i].children.length>0)
return serachById(id,data[i].children);
};
return null;
}
console.log(serachById(0,data));
Here is another simple solution using object notation.
This solution will work even if you decide to get rid of teh array and use object notation later on. so the code will remain the same.
It will also support the case when you have element with no children.
function findId(obj, id) {
var current, index, reply;
// Use the object notation instead of index.
for (index in obj) {
current = obj[index];
if (current.id === id) {
return current;
}
reply = findId(current.children, id);
if (reply) {
return reply;
}
// If you reached this point nothing was found.
console.log('No match found');
}
}
console.log(findId(data, 319291392));
do it so:
for (var obj in arr) {
if(arr[obj].id== id) {
console.log(arr[obj]);
}
}
I have a JSON data structure as shown below:
{
"name": "World",
"children": [
{ "name": "US",
"children": [
{ "name": "CA" },
{ "name": "NJ" }
]
},
{ "name": "INDIA",
"children": [
{ "name": "OR" },
{ "name": "TN" },
{ "name": "AP" }
]
}
]
};
I need to change the key names from "name" & "children" to say "key" & "value". Any suggestion on how to do that for each key name in this nested structure?
I don't know why you have a semicolon at the end of your JSON markup (assuming that's what you've represented in the question), but if that's removed, then you can use a reviver function to make modifications while parsing the data.
var parsed = JSON.parse(myJSONData, function(k, v) {
if (k === "name")
this.key = v;
else if (k === "children")
this.value = v;
else
return v;
});
DEMO: http://jsfiddle.net/BeSad/
Try this:
function convert(data){
return {
key: data.name,
value: data.children.map(convert);
};
}
Or if you need to support older browsers without map:
function convert(data){
var children = [];
for (var i = 0, len = data.children.length; i < len; i++){
children.push(convert(data.children[i]));
}
return {
key: data.name,
value: children
};
}
You could use a function like this :
function clonerename(source) {
if (Object.prototype.toString.call(source) === '[object Array]') {
var clone = [];
for (var i=0; i<source.length; i++) {
clone[i] = goclone(source[i]);
}
return clone;
} else if (typeof(source)=="object") {
var clone = {};
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
var newPropName = prop;
if (prop=='name') newPropName='key';
else if (prop=='children') newPropName='value';
clone[newPropName] = clonerename(source[prop]);
}
}
return clone;
} else {
return source;
}
}
var B = clonerename(A);
Note that what you have isn't a JSON data structure (this doesn't exist as JSON is a data-exchange format) but probably an object you got from a JSON string.