te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>arrays - How to convert object properties string to integer in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How to convert object properties string to integer in javascript - Stack Overflow

programmeradmin2浏览0评论

I would like to know how to convert object properties string to integer in javascript. I have a obj, which if has property value is number string convert to number in javascript

var obj={
  ob1: {id: "21", width:"100",height:"100", name: "image1"},
  ob2: {id: "22", width:"300",height:"200", name: "image2"}
}


function convertIntObj (obj){
    Object.keys(obj).map(function(k) { 
            if(parseInt(obj[k])===NaN){
              return obj[k] 
            }
            else{
              return parseInt(obj[k]);
            }
        });
}

var result = convertIntObj(obj);

console.log(result)

I would like to know how to convert object properties string to integer in javascript. I have a obj, which if has property value is number string convert to number in javascript

var obj={
  ob1: {id: "21", width:"100",height:"100", name: "image1"},
  ob2: {id: "22", width:"300",height:"200", name: "image2"}
}


function convertIntObj (obj){
    Object.keys(obj).map(function(k) { 
            if(parseInt(obj[k])===NaN){
              return obj[k] 
            }
            else{
              return parseInt(obj[k]);
            }
        });
}

var result = convertIntObj(obj);

console.log(result)

Expected Output:

[
  {id: 21, width:100,height:100, name: "image1"},
  {id: 22, width:300,height:200, name: "image2"}
]
Share Improve this question edited Apr 6, 2020 at 10:19 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Apr 6, 2020 at 10:17 SenSen 7733 gold badges14 silver badges35 bronze badges 2
  • 1 You don't have any return in convertIntObj, so the value of the function call is implicitly undefined – VLAZ Commented Apr 6, 2020 at 10:20
  • 2 ... and NaN is not equal to anything, not even to itself. Also, you need to map the properties of obj individually, currently you're parsing the objects in obj, not their properties. – Teemu Commented Apr 6, 2020 at 10:21
Add a ment  | 

4 Answers 4

Reset to default 10

This should do the work:

var obj = {
  ob1: {
    id: "21",
    width: "100",
    height: "100",
    name: "image1"
  },
  ob2: {
    id: "22",
    width: "300",
    height: "200",
    name: "image2"
  }
}


function convertIntObj(obj) {
  const res = {}
  for (const key in obj) {
    res[key] = {};
    for (const prop in obj[key]) {
      const parsed = parseInt(obj[key][prop], 10);
      res[key][prop] = isNaN(parsed) ? obj[key][prop] : parsed;
    }
  }
  return res;
}

var result = convertIntObj(obj);

console.log('Object result', result)

var arrayResult = Object.values(result);

console.log('Array result', arrayResult)

Click "Run code snippet" so see the result

Iterating over Object.keys() twice. If the value corresponding to the key is a number then parseInt the value else set the default value which was present earlier

var obj = {
  ob1: { id: "21", width: "100", height: "100", name: "image1" },
  ob2: { id: "22", width: "300", height: "200", name: "image2" }
};

var res = {};

Object.keys(obj).forEach(key => {
  res[key] = {};
  Object.keys(obj[key]).forEach(temp => {
    res[key][temp] = !isNaN(obj[key][temp])
      ? parseInt(obj[key][temp], 10)
      : obj[key][temp];
  });
  return res;
});

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can use Object.entries() and .reduce() methods to iterate over the key value pairs in your data and use Number and Number.isNaN() methods to transform the values appropriately.

const data = {
  ob1: {id: "21", width:"100",height:"100", name: "image1"},
  ob2: {id: "22", width:"300",height:"200", name: "image2"}
};

const result = Object.entries(data).reduce((r, [k, o]) => {
    r[k] = Object.entries(o).reduce((r, [k, v]) => {
        let _v = Number(v);
        if(!Number.isNaN(_v)) { v = _v; }
        return (r[k] = v, r);
    }, {});
    return r;
}, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Hi I would remend you to use the JSON.stringify() method. It is used to convert object to string which is needed to send data over the web server. It converts the set of variables in the object to a JSON string:

var objToStr = {
  siteName: "W3Docs",
  bookName: "Javascript",
  booksCount: 5
};
var myJSON = JSON.stringify(objToStr);
console.log(myJSON);

Also, you can use the The toString() method. It is also called when you need to convert the object into a string:

var obj = {
  siteName: "W3Docs",
  bookName: "Javascript",
  booksCount: 5
};
function objToString(object) {
  var str = '';
  for (var k in object) {
    if (object.hasOwnProperty(k)) {
      str += k + '::' + object[k] + '\n';

This information is taken from this source.

发布评论

评论列表(0)

  1. 暂无评论