JavaScript Using Array.prototype.filter to filter nested children objects using array.prototype.find,using array.prototype in javascript,why

I have an array of objects similar to the following block of code:

var arr = [
	{
    	text: 'one',
        children: [
        	{
            	text: 'a',
                children: [
                	{
                        text: 'something'
                    }
                ]
            },
            {
            	text: 'b'
            },
            {
            	text: 'c'
            }
        ]
    },
    {
    	text: 'two'
    },
    {
    	text: 'three'
    },
    {
    	text: 'four'
    }
];
Answer:1

I suggest to use Array#forEach, because filter returns an array, which is needed, but not practical for this purpose, because it returns all children with it.

This proposal generates a new array out of the found items, with the wantes item text and children.

The solution works iterative and recursive. It finds all occurences of the search string.

function filter(array, search) {
    var result = [];
    array.forEach(function (a) {
        var temp = [],
            o = {},
            found = false;

        if (a.text === search) {
            o.text = a.text;
            found = true;
        }
        if (Array.isArray(a.children)) {
            temp = filter(a.children, search);
            if (temp.length) {
                o.children = temp;
                found = true;
            }
        }
        if (found) {
            result.push(o);
        }
    });
    return result;
}

var array = [{ text: 'one', children: [{ text: 'a', children: [{ text: 'something' }] }, { text: 'b' }, { text: 'c' }] }, { text: 'two' }, { text: 'three' }, { text: 'four' }];

console.log(filter(array, 'something'));
Answer:2

Wanted to use the 'uncover' effect on a slideshow but it seems that it doesn't work using jQuery Cycle 2. It is working fine using the old jQuery Cycle plugin. Please see code here $(function() { ...

Wanted to use the 'uncover' effect on a slideshow but it seems that it doesn't work using jQuery Cycle 2. It is working fine using the old jQuery Cycle plugin. Please see code here $(function() { ...

Write a function addArrays that takes 2 arrays of numbers as parameters and returns a new array where elements at same index position are added together. For example: addArrays([1,2,3], [4,3,2,1]); // ...

Write a function addArrays that takes 2 arrays of numbers as parameters and returns a new array where elements at same index position are added together. For example: addArrays([1,2,3], [4,3,2,1]); // ...

Is it possible to do this with html canvas? With this for (var i = 0; i < data.length; i += 4) { var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722; data[i] ...

Is it possible to do this with html canvas? With this for (var i = 0; i < data.length; i += 4) { var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722; data[i] ...

I am using tableexport.js to export my html table into different formats like excel, csv, pdf etc. In tableexport.js, there is code for filename and extension which is not working. window.open('...

I am using tableexport.js to export my html table into different formats like excel, csv, pdf etc. In tableexport.js, there is code for filename and extension which is not working. window.open('...

  1. tableexport jquery plugin download
  2. tableexport.jquery.plugin
  3. tableexport.jquery.plugin example
  4. tableexport.jquery.plugin cdn
  5. tableexport.jquery.plugin/html2canvas.js
  6. tableexport.jquery.plugin npm
  7. tableexport.jquery.plugin xlsx
  8. tableexport.jquery.plugin-master
  9. tableexport.jquery.plugin demo
  10. tableexport.jquery.plugin not working
  11. tableexport.jquery.plugin header
  12. tableexport.jquery.plugin/tableexport.min.js
  13. kayalshri/tableexport.jquery.plugin
  14. github table export jquery plugin
  15. html table export jquery plugin
  16. html table export jquery plugin example
  17. tableexport.jquery.plugin/master/tableexport.js
  18. how to use tableexport.jquery.plugin
  19. tableexport.jquery.plugin hhurz