How to Deep Clone an Array in JavaScript

let array_A = [
    {'id': 1, 'type': true},
    {'id': 2, 'type': false},
    {'id': 3, 'type': true},
    {'id': 4, 'type': false},
    {'id': 5, 'type': true},
];


/**
 *  方法一: jQuery (若有使用 jQuery 可考慮)
 */

let array_B = $.extend(true, {}, array_A);

// Convert object to array
array_B = Object.values(array_B);

/**
 *  方法二: JSON.parse(JSON.stringify())
 *  (純資料,可行;若遇 Function、Set、Map..等型態,失效)
 */

let array_B = JSON.parse(JSON.stringify(array_A));

/**
 *  方法三: 使用 lodash (library)
 */

let array_B = _.cloneDeep(array_A);

/**
 *  方法四: 國外網友提供的遞迴方法,感覺很不錯
 */

const clone = (items) => items.map(item => Array.isArray(item) ? clone(item) : {...item});
let array_B = clone(array_A);

// Select type is true
array_B = array_B.filter( number => number.type );

// Set type to false
array_B.map( number => number.type = false );

console.log(array_A);
console.log(array_B);

https://dev.to/samanthaming/how-to-deep-clone-an-array-in-javascript-3cig

https://kanboo.github.io/2018/01/27/JS-ShallowCopy-DeepCopy/

Nginx Config

nginx.conf
user nginx;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    use epoll;
    multi_accept on;
    worker_connections 65535;
}

http {
    # DDoS Defense
    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;
    limit_conn_status 429;
    limit_req_status 429;

    client_body_buffer_size  128k;
    client_header_buffer_size 3m;
    large_client_header_buffers 4 256k;
    client_body_timeout   5;
    client_header_timeout 5;

    reset_timedout_connection on;
    send_timeout 5;
    keepalive_timeout 5 5;
    keepalive_requests 100000;

    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    log_not_found off;
    types_hash_max_size 2048;
    client_max_body_size 100M;

    # MIME
    include mime.types;
    default_type application/octet-stream;

    # logging
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log warn;

    # load configs
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}
default.conf
server {
    listen 80 default_server;
    server_name _;
    return 444; #Nginx No Response
}
general.conf
# security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

# . files
location ~ /\.(?!well-known) {
    deny all;
}
nginx/sites-available/your_domain_name.conf
server {
    listen 80;
    server_name www.domain.com;
    return 301 https://domain.com$request_uri;
}

server {
    listen 80;
    server_name domain.com;

    root /var/www/html/domain/public;

    if ($http_x_forwarded_proto = "http") {
        return 301 https://$server_name$request_uri;
    }

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
        limit_conn conn_limit_per_ip 10;
        limit_req zone=req_limit_per_ip burst=10 nodelay;
    }

    include general.conf;
}

解決 laradock pathinfo PATHINFO_BASENAME 不支持中文開頭的檔名

  • 先透過 locale -a 查詢作業系統支援那些語系編碼
  • 若未安裝額外語系編碼,則至 laradock .env 設置
    • PHP_FPM_INSTALL_ADDITIONAL_LOCALES=true
    • PHP_FPM_ADDITIONAL_LOCALES=”zh_TW.UTF-8″
  • 重新編譯 PHP-FPM:docker-compose build php-fpm
  • setlocale(LC_ALL,’zh_TW.utf8′);