过滤阵列数据

时间:2019-09-18 06:12:23

标签: javascript

我是ES6的新手,我有一个问题,我有一个嵌套数组,我想根据选中的复选框输入来过滤数据。

$a = '{"id":41,"userId":9,"description":null,"updated_by":"Juan Dela Cruz","created_at":"2019-09-18 10:07:34","updated_at":"2019-09-18 10:07:34"}';
$b = '{"id":2,"userId":9,"description":null,"updated_by":null,"created_at":"2019-09-16 12:46:56","updated_at":"2019-09-16 12:46:56"}';
$arr1 = json_decode($a, true);
$key1 = strtotime($arr1['created_at']);
$arr[$key1] = $arr1;
$arr2 = json_decode($b, true);
$key2 = strtotime($arr2['created_at']);
$arr[$key2] = $arr2;
//print_r($arr);
$key = max(array_keys($arr));
print_r($arr[$key]);

所以我需要得到:

  

案例I::如果选中了 X5 并且是唯一的孩子,我需要获取父母的信息:

const data = [
    { name: 'Airplane', type: 'folder', folderId: 1 },
    {
        name: 'Car',
        type: 'folder',
        folderId: 2,
        children: [
            {
                Name: 'BMW',
                type: 'folder',
                folderId: 3,
                children: [{ Name: 'X5', type: 'file', folderParentId: 3 }]
            },
            { Name: 'Toyota', type: 'file', folderParentId: 2 },
            { Name: 'Mazda', type: 'file', folderParentId: 2 },
            { Name: 'Ferrari', type: 'file', folderParentId: 2 }
        ]
    }
]

  

案例II::如果选中了 Toyota和Ferrari ,我需要获取所选的孩子:

{
    Name: 'BMW',
    type: 'folder',
    folderId: 3
}
  

案例III::如果选中了所有子级和所有子文件夹,我需要获取选定的祖父母:

{
    Name: 'Toyota',
    type: 'file',
    folderParentId: 2
},
{
    Name: 'Ferrari',
    type: 'file',
    folderParentId: 2
}

复选框的功能后,我得到了一个像这样的数组:

{
    name: 'Car',
    type: 'folder',
    folderId: 2,
}

我尝试:

const checked = [
{ name: 'Car', type: 'folder', folderId: 2}
{ name: 'BMW', type: 'folder', folderId: 2}
{ Name: 'X5', type: 'file', folderParentId: 3 }
{ Name: 'Toyota', type: 'file', folderParentId: 2 }
{ Name: 'Ferrari', type: 'file', folderParentId: 2 }
]

1 个答案:

答案 0 :(得分:0)

您可以使用下面的函数来获取请求的对象。

#!/use/bin/env python3
import os
import tkinter
import webbrowser
from tkinter import *
from tkinter.filedialog import asksaveasfilename
from tkinter.scrolledtext import ScrolledText
from PIL import Image, ImageTk
from PIL import *

window = tkinter.Tk()
window.geometry("520x800")
window.title("STARLABS BIOSCIENCE SDN BHD")
window.resizable(False, False)
window.config(background="#150051")

MENUBAR = Menu(window)
window.config(menu=MENUBAR)

image = Image.open("s.jpg")
photo = ImageTk.PhotoImage(image)
label = Label(window, image=photo, text="").pack()
venuelabel = Label(window, text="Venue: ", background="#150051", font=("bold", 13,)).place(x=0, y=660)
contactLabel = Label(window, text="Contact: ").place(x=0, y=720)
dateTEXT = Entry(window, width=35, ).place(x=170, y=380)
Venue = ScrolledText(window, width=35, background="#150051").place(x=57, y=660, height=60)
person = Entry(window, width=35).place(x=57, y=720)


# function :
def exitWindow():
    window.destroy()


def save():
    print("save")
    dialogue = image.filename = asksaveasfilename(initialdir="/", title="Select file", filetypes=(
        ('JPEG', ('*.jpg', '*.jpeg', '*.jpe')), ('PNG', '*.png'), ('BMP', ('*.bmp', '*.jdib')), ('GIF', '*.gif')))
    image.save("picture.jpg")


# Add Menu Items:
file_menu = Menu(MENUBAR, tearoff=0)
file_menu.add_cascade(label="Save", command=save)
addon = Menu(file_menu, tearoff=0)
addon.add_command(label="SAVE TO FILE")
addon.add_command(label="Email To")
file_menu.add_command(label="Exit", command=exitWindow)
MENUBAR.add_cascade(label="File", menu=file_menu)

window.mainloop()

您可以测试function getObject(array, key, value) { var o; array.some(function iter(a) { if (a[key] === value) { o = a; return true; } return Array.isArray(a.children) && a.children.some(iter); }); return o; }